iPhone - iPhone 锁定时 NSTimer 问题

发布于 2024-12-01 17:55:55 字数 439 浏览 3 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(2

娜些时光,永不杰束 2024-12-08 17:55:55

我认为除了 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

走过海棠暮 2024-12-08 17:55:55

这就是 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.

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