【发布时间】:2013-11-07 15:04:21
【问题描述】:
我的应用有时会在后台崩溃并显示以下崩溃日志:
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
)}
查看其他questions,我发现崩溃消息表明我没有正确结束任务,所以当时间到期时,操作系统结束了它并崩溃了我的应用程序。
所以我添加了一些 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];
}
}
发现任务在crash的时候被调用了两次:
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];
}
}
}
【问题讨论】:
-
你能发布 pauseDownloads 方法吗?
-
当然,我编辑了问题
-
检查你的initBackground方法,下载完成后结束后台任务。在 iOS 7 中,后台任务最长为 3 分钟,在 iOS 6 中约为 10 分钟。