在后台每隔X分钟执行一次功能不起作用 [英] Execute function every X minutes in background doesn't work

查看:97
本文介绍了在后台每隔X分钟执行一次功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码每X分钟执行一次函数:

I use this code to execute function every X minutes:

- (void)executeEveryOneMinute
{
    [self myFunction];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(60 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self executeEveryOneMinute];

    });
}

当app在前台时,它可以正常工作。

但是当应用程序进入后台时它不再起作用。

当我再次将应用程序返回到前台时,它执行一次功能。

并继续每分钟再次调用函数。


那么如何让它在后台工作呢?

And it works when app is in foreground.
But when app goes background it doesn't work anymore.
When I return app to foreground again it execute function once.
And continue to call function every minute again.

So how to make this to work in background too?

推荐答案

查看后台执行和多任务部分/ apple_ref / doc / uid / TP40007072-CH4-SW1rel =nofollow> iOS应用程序编程指南:应用程序状态和多任务处理,讨论可能性。例如,您可以让应用程序在后台运行几分钟,以完成一些有限长度的任务。或者你可以继续在后台运行应用程序,如果它正在执行一个非常特殊的功能列表(引用上述文档):

See the Background Execution and Multitasking section of the iOS App Programming Guide: App States and Multitasking for a discussion of the possibilities. You can, for example, keep the app running in the background for a few minutes in order to complete some finite length task. Or you can continue to run the app in the background for a longer period of time if it's performing one of a very particular list of functions (quoting from the aforementioned document):



  • 在后台播放用户可听内容的应用,例如音乐播放器应用

  • 录制音频内容的应用在后台。

  • 随时向用户通知其位置信息的应用,例如导航应用

  • 支持互联网语音的应用协议(VoIP)

  • 需要定期下载和处理新内容的应用程序

  • 从外部附件接收定期更新的应用程序

  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that record audio content while in the background.
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that support Voice over Internet Protocol (VoIP)
  • Apps that need to download and process new content regularly
  • Apps that receive regular updates from external accessories

实施这些服务的应用程序必须声明它们支持的服务,并使用系统框架来实现这些服务的相关方面。声明服务可以让系统知道您使用哪些服务,但在某些情况下,系统框架实际上会阻止您的应用程序被暂停。

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

但是,iOS电池/电源管理的基本设计原则是随机应用程序不能(也不应该)继续在后台运行。如果您正在分享您正在尝试做的事情(即,您在 executeEveryOneMinute 方法中正在做什么),我们可以就如何实现期望的效果,如果可能的话。

But, a fundamental design principle in iOS battery/power management is that random apps can not (and should not) continue to run in the background. If you share what precisely you're trying to do (namely, what precisely you're doing inside that executeEveryOneMinute method), though, we can offer counsel on how to achieve the desired effect, if possible.

如果您尝试在后台继续上传,请在iOS 7和更好,你应该考虑使用 NSURLSession 与后台会话配置( [NSURLSessionConfiguration backgroundSessionConfiguration:identifier] ;有类似的iOS 8中的方法。这将继续尝试上传(自动,无需您的进一步干预),不仅在您的应用程序离开前台后,甚至在应用程序终止后(例如由于内存压力或崩溃)。 AFNetworking提供基于 NSURLSession 的类, AFURLSessionManager ,它支持此功能(虽然它不是 NSOperation - )。通过这种方式,您可以享受背景上传,但符合Apple关于背景操作的指导原则,特别是电池效果不如每60秒重试一次。

If you're trying to have an upload continue in the background, in iOS 7 and greater, you should consider using NSURLSession with a background session configuration ([NSURLSessionConfiguration backgroundSessionConfiguration:identifier]; there is a similar method in iOS 8). This will continue to attempt to upload (automatically, without further intervention on your part) not only after your app has left the foreground, but even after the app is terminated (e.g. due to memory pressure or a crash). AFNetworking offers a NSURLSession-based class, AFURLSessionManager, which supports this (though it's not NSOperation-based). This way, you enjoy background uploads, but conforms to Apple guidelines on background operation, notably with less dramatic battery impact than retrying yourself every 60 seconds.

我建议你请参阅WWDC 2013视频的后半部分基础网络的新功能 ,它演示了这个过程(他们正在进行下载,但上传的想法是一样的。)

I'd suggest you refer to the latter part of WWDC 2013 video What’s New in Foundation Networking, which demonstrates this process (they're doing a download, but the idea is the same for uploads).

这篇关于在后台每隔X分钟执行一次功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆