![](/img/trans.png)
[英]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.