简体   繁体   English

是否可以使用NSTimer在后台刷新我的应用程序?

[英]Is it impossible to use a NSTimer for refresh my app in background?

I have a question about application background refresh. 我对应用程序背景刷新有疑问。

In my AppDelegate.swift, I use an NSTimer to refresh my app when my app enters background mode. 在我的AppDelegate.swift中,当我的应用程序进入后台模式时,我使用NSTimer刷新我的应用程序。

On the iOS Simulator, refresh works perfectly in background mode, but when I run my app on the real device, refresh doesn't work. 在iOS模拟器上,刷新可以在后台模式下完美运行,但是当我在真实设备上运行我的应用程序时,刷新不起作用。

Is it impossible to use a NSTimer for refresh my app in background? 是否可以使用NSTimer在后台刷新我的应用程序?

Your application may not stay long in the 'Background' state. 您的应用程序可能不会长时间处于“背景”状态。 It could go to 'Suspended' or be killed by the system, if it wants to. 如果愿意,它可以转到“挂起”或被系统杀死。 And of course, no code from your app can run in those states. 当然,您的应用中没有任何代码可以在这些状态下运行。

That said, I think it's not a good practice to use NSTimer for scheduling background work, and use NSNotification instead. 也就是说,我认为使用NSTimer安排后台工作而不是使用NSNotification不是一个好习惯。

I recommend to read thoroughly the Energy Efficiency Guide for iOS Apps . 我建议您仔细阅读iOS应用能源效率指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM