iPhone - iPhone 锁定时 NSTimer 问题
我有 NSTimer 每 15 秒检查一次来自服务器的最新数据。 当应用程序进入后台时,我将在以下应用程序委托方法中停止计时器
- (void)applicationWillResignActive:(UIApplication *)application
当应用程序返回活动状态时,我将激活计时器,它会再次活动
- (void)applicationWillEnterForeground:(UIApplication *)application
此功能工作正常。 但当我离开 iPhone 时,它会在一段时间后锁定。 当我解锁它时,该功能无法正常继续。计时器未运行。
我做错了什么?
我不希望计时器在后台运行。这就是为什么在进入后台之前,我禁用计时器,而当进入前台时,我想重新启动计时器。
I am having NSTimer that check for latest data from server every 15 seconds.
When the app goes to background, i am stopping the timer in following app delegate method
- (void)applicationWillResignActive:(UIApplication *)application
When the app comes back to active, i am activating timers and it becomes active again
- (void)applicationWillEnterForeground:(UIApplication *)application
This functionality works fine.
But when I leave the iPhone, it locks after some time.
When I unlock it, the functionality is not continuing properly. Timer is not running.
What am I doing wrong?
I don't want timers to run in background. That's why before going to background, i'm disabling the timer and when comes to foreground, i want to restart timer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为除了 applicationWillEnterForeground 之外,您可能还需要一个额外的委托方法。我认为当用户解锁手机时,您的应用程序已经在前台,因此不会进入它。但我不确定委托方法是什么,也许 应用程序已变为活动状态
I think you may need an additional delegate method other than applicationWillEnterForeground. I think that when the user unlocks their phone, your app will already be in the foreground and so will not enter it. But I'm not sure what the delegate method will be, maybe applicationDidBecomeActive
这就是 Apple 后台处理的方式 有效...
您可以创建一个后台任务,这将使 NSTimer 在后台工作,但它们的时间有限,您不能让它始终在后台工作。
That is just how the Apple background processing works...
You can create a background task which will make the NSTimer's work in the background BUT they are limited in time, you can't have it working always in the background.