Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IOs7应用程序在后台崩溃_Ios_Crash_Ios7 - Fatal编程技术网

IOs7应用程序在后台崩溃

IOs7应用程序在后台崩溃,ios,crash,ios7,Ios,Crash,Ios7,我的应用有时在后台崩溃,并显示以下崩溃日志: 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 applicationDid

我的应用有时在后台崩溃,并显示以下崩溃日志:

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 
    )}
发现任务崩溃时被调用了两次:

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

您应该在没有过期的情况下结束后台

    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;
当应用程序进入前台时,将此平衡
endBackgroundTask
call-

- (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
调用之前

(另一个潜在问题)


另外,我注意到您的PausedDownloads代码现在,当应用程序进入后台时,您似乎正在使用NSOperationQueue执行下载。在这种情况下,请确保在完成下载或执行过期处理程序之前,不会调用
endBackgroundTask
。换句话说,控件不会过早返回,这意味着您可能需要监视这些下载操作。

简单的回答是,发生崩溃是因为您创建了两个后台任务:7和8,但只结束了8


有了这些知识,您应该应用Ashok的解决方案来确保始终结束您创建的任何后台任务。

您可以发布PausedDownloads方法吗?当然,我编辑了问题检查您的initBackground方法,并在完成下载后结束后台任务。在iOS 7中,最大的后台任务是3分钟,在iOS 6中大约是10分钟。实际上,这段代码不是我写的,我被指派来修复这个崩溃,而编写它的人在这里不再工作了,所以我不确定他的意图,但我相信任务的结束并不是为了让下载尽可能长时间地运行OK,但这是一个需要解决的有趣问题。我相信你至少需要几个小时来理解和修复它,而不仅仅是几分钟。我建议您尝试在这些NSlog中添加一些时间戳
[NSDate]
,以帮助您调试和理解。注意-好的,如果NSOperationQueue未对其进行限制,则每个NSOperation都将在单独的线程中执行。当你有空的时候,你可以阅读这个链接-我会尝试一下,谢谢,还有关于任务被调用两次的想法吗?我刚注意到在你的日志开始调试时间戳。做一件事,将一个平衡
endBackgroundTask
调用放入
applicationWillEnterForeground
delegate方法。看起来只有当应用程序频繁地从前台切换到后台时才会发生崩溃!!??是的,崩溃就是这样发生的,当应用程序频繁地从前台切换到后台时,我尝试了你的建议,我需要运行更多的测试,但似乎已经解决了问题,谢谢
    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;
- (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).
}