ios – beginBackgroundTaskWithExpirationHandler调用endBackgroundTask但没有结束进程

ios – beginBackgroundTaskWithExpirationHandler调用endBackgroundTask但没有结束进程,第1张

概述即使应用程序在后台运行,我也有一些长时间运行的进程.我正在调用应用程序的beginBackgroundTaskWithExpirationHandler:方法,并在expirationBlock中调用应用程序的endBackgroundTask. 这是实施: __block UIBackgroundTaskIdentifier task = [[UIApplication sharedApplic 即使应用程序在后台运行,我也有一些长时间运行的进程.我正在调用应用程序的beginBackgroundTaskWithExpirationHandler:方法,并在expirationBlock中调用应用程序的endBackgroundTask.
这是实施:
__block uibackgroundtaskIDentifIEr task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{    [[UIApplication sharedApplication] endBackgroundTask:task];    task = uibackgroundtaskInvalID;}];dispatch_queue_t queue = dispatch_queue_create("com.test.test1234",disPATCH_QUEUE_SERIAL);dispatch_async(queue,^{    // My Task goes here});

在某些情况下,我的串行队列有更多的任务要执行,这些任务无法在系统提供的时间内完成.所以到期块将执行,因为我结束了uibackgroundtaskIDentifIEr但没有停止调度过程(我甚至无法取消调度).

Apple的文件说:

Each call to the beginBackgroundTaskWithname:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method generates a unique token to associate with the corresponding task. When your app completes a task,it must call the endBackgroundTask: method with the corresponding token to let the system kNow that the task is complete. Failure to call the endBackgroundTask: method for a background task will result in the termination of your app. If you provIDed an expiration handler when starting the task,the system calls that handler and gives you one last chance to end the task and avoID termination.

所以,根据这个,如果我没有调用endBackgroundTask:我的应用程序将被终止,这是好的.

我的问题是:使用我当前的实现,如果我调用endBackgroundTask:在expirationHandler块中,我的调度队列的任务没有完成?我的应用将被终止或暂停?

谢谢

解决方法 下面是一些场景,您需要在使用beginBackgroundTaskWithExpirationHandler时处理,否则您的应用将终止.

场景1:您的应用程序正在Foreground中运行.你开始beginBackgroundTaskWithExpirationHandler然后进入后台模式.你的应用程序保持活力很久.

场景2:您的应用程序正在Foreground中运行.你开始beginBackgroundTaskWithExpirationHandler然后进入后台模式.然后回到Foreground模式并且你没有调用endBackgroundTask然后你的应用程序仍然执行后台队列,因此它将在接下来的3分钟内扩展该过程(在IOS 7介绍之后.在IOS 7之前,过程执行时间是10分钟).所以你必须要取消后台队列和任务从后台队列中出来并进入前台队列.

以下是向您展示的代码.什么是处理后台进程的最佳方式.

步骤1:将__block uibackgroundtaskIDentifIEr bgTask声明为全局变量.

第2步:在applicationDIDEnterBackground中添加以下代码.

- (voID)applicationDIDEnterBackground:(UIApplication *)application {         bgTask = [application beginBackgroundTaskWithExpirationHandler:^{         bgTask = uibackgroundtaskInvalID;          }];}

第3步:一旦应用程序进入前台模式,停止后台任务处理程序.

- (voID)applicationWillEnterForeground:(UIApplication *)application {  // Called as part of the Transition from the background to the active state; here you can undo many of the changes made on entering the background.  [[UIApplication sharedApplication] endBackgroundTask:bgTask];}
总结

以上是内存溢出为你收集整理的ios – beginBackgroundTaskWithExpirationHandler调用endBackgroundTask但没有结束进程全部内容,希望文章能够帮你解决ios – beginBackgroundTaskWithExpirationHandler调用endBackgroundTask但没有结束进程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存