
[英]IOS Geofencing when app is active (foregroundd) and inactive(background) ios7
[英]IOs7 app crashing when in background
我的应用程序有时会在后台崩溃并显示以下崩溃日志:
Nov 7 12:33:31 iPad backboardd[29] <Warning>: MyApp[3096] has active assertions beyond permitted time:
{(
<BKProcessAssertion: 0x14680c60> identifier: Called by MyApp, from -[AppDelegate applicationDidEnterBackground:] process: MyApp[3096] permittedBackgroundDuration: 180.000000 reason: finishTask owner pid:3096 preventSuspend preventIdleSleep preventSuspendOnSleep
)}
通过其他问题我发现崩溃消息表明我没有正确结束任务,所以当它的时间到期时,操作系统结束了它并使我的应用程序崩溃。
所以我添加了一些NSLog:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self saveContext];
[Settings setCallLock:YES];
[self saveStuff];
if ([self isBackgroundTaskNeeded])
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[self pauseDownloads];
NSLog(@"Debug - Ending background task %d",bgTask);
[app endBackgroundTask:bgTask];
NSLog(@"Debug - Background task %d ended",bgTask);
bgTask = UIBackgroundTaskInvalid;
}];
NSLog(@"Debug - Starting background task %d",bgTask);
[self initBackground];
}
}
并发现该任务在崩溃时被调用了两次:
Nov 7 12:30:02 iPad MyApp[3096] <Warning>: Debug - Starting background task 7
Nov 7 12:30:30 iPad MyApp[3096] <Warning>: Debug - Starting background task 8
Nov 7 12:33:26 iPad MyApp[3096] <Warning>: Debug - Ending background task 8
Nov 7 12:33:26 iPad MyApp[3096] <Warning>: Debug - Background task 8 ended
Nov 7 12:33:26 iPad MyApp[3096] <Warning>: Debug - Ending background task 0
Nov 7 12:33:26 iPad MyApp[3096] <Warning>: Debug - Background task 0 ended
我无法分辨双重呼叫何时发生以及原因。 所以问题是为什么任务被调用两次并且有办法避免它吗?
编辑:
- (void) pauseDownloads
{
for (AFDownloadRequestOperation *operation in self.operationQueue.operations)
{
if(![operation isPaused])
{
[operation pause];
}
}
}
你应该endBackground没有过期..
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[self pauseDownloads];
NSLog(@"Debug - Ending background task %d",bgTask);
[app endBackgroundTask:bgTask];
NSLog(@"Debug - Background task %d ended",bgTask);
bgTask = UIBackgroundTaskInvalid;
}];
NSLog(@"Debug - Starting background task %d",bgTask);
[self initBackground];
NSLog(@"Debug - Ending background task %d without expiration",bgTask);
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
当app进入前台时,把这个平衡的endBackgroundTask
调用 -
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if (bgTask != UIBackgroundTaskInvalid) {
NSLog(@"Debug - Ending background task %d without expiration (when applicationWillEnterForeground)",bgTask);
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
// Also, to counter expiration handler pauseDownloads call, consider resuming the downloads here (or applicationDidBecomeActive app delegate).
}
因为 - 当后台任务到期时执行到期处理程序块 {}(通常最多10分钟,但不能保证)。 我假设你的实际后台任务是[self initBackground];
,否则你应该把它放在块之外并在endBackgroundTask
调用之前。
(另一个潜在的问题)
此外,我现在注意到你的pauseDownloads代码,当应用程序进入后台时,你似乎正在使用NSOperationQueue执行下载。 在这种情况下,确保在完成下载或执行到期处理程序之前不调用endBackgroundTask
。 换句话说,控件不会过早返回,这意味着您可能需要监视这些下载NSOperation(s)。
简短的回答是你的崩溃发生了,因为你创建了两个后台任务:7和8但只结束了8。
有了这些知识,您应该应用Ashok的解决方案,以确保您始终结束您创建的任何后台任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.