在视图生命周期中哪里使 NSTimer 失效?

发布于 2024-10-20 13:03:39 字数 947 浏览 2 评论 0原文

我有一个奇怪的问题,使 NSTimer 无效。只要用户在特定的屏幕上,我就需要不断地更新它。我正在使用 NSTimer 来完成它。我在 viewDidLoad 方法中编写了以下代码。

- (void)viewDidLoad {
self.pollServerForUpdates = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(fetchNewDataFromServer:)
userInfo:nil
repeats:YES];
}

问题是当我尝试使计时器无效时。由于我希望应用程序在用户离开屏幕时停止轮询服务器,因此我将计时器失效代码放在 viewWillDisappear 方法中。

-(void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:YES];
//NSLog(@"%d",[self.view retainCount]);
[self.pollServerForUpdates invalidate];
self.pollServerForUpdates = nil;
}

我使用导航控制器在主视图和轮询视图之间来回切换。如果我在主视图和轮询视图之间快速移动,应用程序就会崩溃。我启用了 NSZombie 来查看发生了什么,这就是我得到的结果,

 *** -[CALayer retainCount]: message sent to deallocated instance 0x1c3be0

如果我删除计时器失效,我的应用程序可以正常工作。但我想在用户离开屏幕后立即停止轮询(计时器)。

我相信这种情况正在发生,因为计时器在视图释放之前被调用,我该如何避免它?我需要改变我的设计吗?任何帮助将不胜感激。

ps:我无法在此屏幕上使用推送通知。

I have a weird problem invalidating NSTimer. As long as the user is on a particular screen, I need to constantly update it. I'm using NSTimer to accomplish it. I wrote the below piece of code in viewDidLoad method.

- (void)viewDidLoad {
self.pollServerForUpdates = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(fetchNewDataFromServer:)
userInfo:nil
repeats:YES];
}

Problem is when I try to invalidate the timer. As I want the app to stop polling the server when the user leaves the screen, I put the timer invalidation code in viewWillDisappear method.

-(void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:YES];
//NSLog(@"%d",[self.view retainCount]);
[self.pollServerForUpdates invalidate];
self.pollServerForUpdates = nil;
}

I use a navigation controller to go back and forth between my main view and the polling view. The app crashes if I move between my main view and polling view real fast. I enabled the NSZombie to see whats happening and this is what I get

 *** -[CALayer retainCount]: message sent to deallocated instance 0x1c3be0

If I remove the timer invalidation my app works fine. But I want to stop the polling (timer) as soon as the user leaves the screen.

I believe this is happening because the timer is called a split second before the view is released, how do I avoid it? Do I need to change my design? Any help will be appreciated.

p.s: I can't use push notifications on this screen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴随着你 2024-10-27 13:03:39

该错误可能在其他地方,其他一些类正在使用您的视图而不持有对其的引用。如果您不使计时器无效,那么它将永远引用您的视图,从而有可能延长其生命周期并掩盖代码中其他地方的内存管理错误。

尝试打破异常,看看对僵尸的调用来自哪里。

It may be that the bug is somewhere else, with some other class that is using your view without holding a reference to it. If you don't invalidate your timer than it will have a reference to your view forever, potentially extending its lifespan and masking memory management bugs elsewhere in your code.

Try breaking on exceptions, and see where the call to the zombie is coming from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文