如何在 iOS 终止应用程序之前在过期处理程序中优雅地结束任务?
我们希望在调用过期处理程序后注销我们的应用程序。这在某些时候有效,但有时即使我们通过调用 endBackgroundTask 结束后台任务,它也会硬终止进程。在这种情况下,我们的注销过程似乎花费了太长的时间,iOS 就直接终止了它。这应该不是一项很长的任务,但是有什么办法可以请求额外的时间吗?
if ([app respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
// Synchronize the cleanup call on the main thread in case
// the task actually finishes at around the same time.
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid) {
backgroundKilled = true;
[self logoutAll];
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}];
}
We want to logout our app once the expiration handler is invoked. This works some of the time, but it appears there are times when it just kills the process hard even though we end the background task by calling endBackgroundTask. It almost seems like our logout process in this case is taking too long and iOS just kills it. This shouldn't be a long task, but is there any way to request additional time?
if ([app respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
// Synchronize the cleanup call on the main thread in case
// the task actually finishes at around the same time.
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid) {
backgroundKilled = true;
[self logoutAll];
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个很好的 UIApplication 委托概述,涵盖了应用程序在终止时所经历的情况。
Here is a good UIApplication delegate overview, covers what the app goes through when being terminated.
iOS 为您提供十分钟的后台任务时间。您的注销过程需要那么长时间吗?
iOS gives you ten minutes for your background task. Is your logout process taking that long?