UIKitBackgroundCompletionTask-iPhone应用程序崩溃

UIKitBackgroundCompletionTask-iPhone应用程序崩溃,iphone,objective-c,Iphone,Objective C,我面临一个问题,还没有任何解决办法。在用户按下Home按钮后,我正在使用后台任务处理程序启动一些数据获取。代码类似于: -(void)startRequest { UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ dispatch_async(dispatch_get_main_queu

我面临一个问题,还没有任何解决办法。在用户按下Home按钮后,我正在使用后台任务处理程序启动一些数据获取。代码类似于:

-(void)startRequest {
    UIApplication *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
        dispatch_async(dispatch_get_main_queue(), ^{
                [app endBackgroundTask:bgTask];
                bgTask = UIBackgroundTaskInvalid;
            });
    }];
    //..
    //Fetch data with NSURLRequest / delegate method
    //..
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    if ([delegate respondsToSelector:delegateErrorMethod])
        [delegate performSelector:delegateErrorMethod];

    UIApplication *app = [UIApplication sharedApplication];
    if (bgTask != UIBackgroundTaskInvalid) {
       dispatch_async(dispatch_get_main_queue(), ^{
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        });
    }
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if([delegate respondsToSelector:delegateMethod])
        [delegate performSelector:delegateMethod withObject:self];  

    UIApplication *app = [UIApplication sharedApplication];
    if (bgTask != UIBackgroundTaskInvalid) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        });
    }
}
它工作得很好,但有时(没有任何问题或原因,因此确实不可预测)应用程序会崩溃,崩溃日志如下:

Application Specific Information:
MyBackgroundTest[7745] has active assertions beyond permitted time: 
{(
    <SBProcessAssertion: 0x90e65e0> identifier: UIKitBackgroundCompletionTask process: MyBackgroundTest[7745] permittedBackgroundDuration: 600.000000 reason: finishTask owner pid:7745 preventSuspend  preventIdleSleep 
)}

Elapsed total CPU time (seconds): 0.010 (user 0.010, system 0.000), 100% CPU 
Elapsed application CPU time (seconds): 0.000, 0% CPU

Thread 0:
0   libSystem.B.dylib               0x33b5d268 mach_msg_trap + 20
1   libSystem.B.dylib               0x33b5f354 mach_msg + 44
2   CoreFoundation                  0x33a48648 __CFRunLoopServiceMachPort + 88
3   CoreFoundation                  0x33a47ed2 __CFRunLoopRun + 350
4   CoreFoundation                  0x33a47c80 CFRunLoopRunSpecific + 224
5   CoreFoundation                  0x33a47b88 CFRunLoopRunInMode + 52
6   GraphicsServices                0x33b0e4a4 GSEventRunModal + 108
7   GraphicsServices                0x33b0e550 GSEventRun + 56
8   UIKit                           0x32099322 -[UIApplication _run] + 406
9   UIKit                           0x32096e8c UIApplicationMain + 664
10  MyBackgroundTest                0x00002d64 0x1000 + 7524
11  MyBackgroundTest                0x00002d18 0x1000 + 7448

Thread 1:
0   libSystem.B.dylib               0x33b89974 kevent + 24
1   libSystem.B.dylib               0x33c33704 _dispatch_mgr_invoke + 88
2   libSystem.B.dylib               0x33c33174 _dispatch_queue_invoke + 96
3   libSystem.B.dylib               0x33c32b98 _dispatch_worker_thread2 + 120
4   libSystem.B.dylib               0x33bd724a _pthread_wqthread + 258
5   libSystem.B.dylib               0x33bcf970 start_wqthread + 0

Thread 2:
0   libSystem.B.dylib               0x33b5d268 mach_msg_trap + 20
1   libSystem.B.dylib               0x33b5f354 mach_msg + 44
2   CoreFoundation                  0x33a48648 __CFRunLoopServiceMachPort + 88
3   CoreFoundation                  0x33a47ed2 __CFRunLoopRun + 350
4   CoreFoundation                  0x33a47c80 CFRunLoopRunSpecific + 224
5   CoreFoundation                  0x33a47b88 CFRunLoopRunInMode + 52
6   WebCore                         0x304df124 RunWebThread(void*) + 332
7   libSystem.B.dylib               0x33bd6886 _pthread_start + 242
8   libSystem.B.dylib               0x33bcba88 thread_start + 0

Thread 3:
0   libSystem.B.dylib               0x33b5d268 mach_msg_trap + 20
1   libSystem.B.dylib               0x33b5f354 mach_msg + 44
2   CoreFoundation                  0x33a48648 __CFRunLoopServiceMachPort + 88
3   CoreFoundation                  0x33a47ed2 __CFRunLoopRun + 350
4   CoreFoundation                  0x33a47c80 CFRunLoopRunSpecific + 224
5   CoreFoundation                  0x33a47b88 CFRunLoopRunInMode + 52
6   Foundation                      0x336465f6 +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:] + 206
7   Foundation                      0x33624192 -[NSThread main] + 38
8   Foundation                      0x3361d242 __NSThread__main__ + 966
9   libSystem.B.dylib               0x33bd6886 _pthread_start + 242
10  libSystem.B.dylib               0x33bcba88 thread_start + 0

Thread 4:
0   libSystem.B.dylib               0x33b8168c select$DARWIN_EXTSN + 20
1   CoreFoundation                  0x33a7f662 __CFSocketManager + 582
2   libSystem.B.dylib               0x33bd6886 _pthread_start + 242
3   libSystem.B.dylib               0x33bcba88 thread_start + 0

Thread 5:
0   libSystem.B.dylib               0x33bd79e0 __workq_kernreturn + 8
1   libSystem.B.dylib               0x33bd7364 _pthread_wqthread + 540
2   libSystem.B.dylib               0x33bcf970 start_wqthread + 0

Thread 6:
0   libSystem.B.dylib               0x33bd79e0 __workq_kernreturn + 8
1   libSystem.B.dylib               0x33bd7364 _pthread_wqthread + 540
2   libSystem.B.dylib               0x33bcf970 start_wqthread + 0

Unknown thread crashed with unknown flavor: 5, state_count: 1
特定于应用程序的信息:
MyBackgroundTest[7745]在允许的时间之外有活动断言:
{(
标识符:UIKitBackgroundCompletionTask进程:MyBackgroundTest[7745]permittedBackgroundDuration:600.000000原因:finishTask所有者pid:7745 preventSuspend preventIdleSleep
)}
已用CPU总时间(秒):0.010(用户0.010,系统0.000),100%CPU
已用应用程序CPU时间(秒):0.000,0%CPU
线程0:
0 libSystem.B.dylib 0x33b5d268马赫/平方米陷阱+20
1 libSystem.B.dylib 0x33b5f354马赫数msg+44
2 CoreFoundation 0x33a48648\uU CFRunLoopServiceMachPort+88
3 CoreFoundation 0x33a47ed2\uuu CFRunLoopRun+350
4 CoreFoundation 0x33a47c80 CFRunLoopRunSpecific+224
5 CoreFoundation 0x33a47b88 CFRunLoopRunInMode+52
6个图形服务0x33b0e4a4 GSEventRunModal+108
7个图形服务0x33b0e550 GSEventRun+56
8 UIKit 0x32099322-[UIApplication_run]+406
9 UIKit 0x32096e8c UIApplicationMain+664
10我的背景测试0x00002d64 0x1000+7524
11我的背景测试0x00002d18 0x1000+7448
线程1:
0 libSystem.B.dylib 0x33b89974 kevent+24
1 libSystem.B.dylib 0x33c33704调度管理调用+88
2 libSystem.B.dylib 0x33c33174 _调度_队列_调用+96
3 libSystem.B.dylib 0x33c32b98 _调度_工人_线程2+120
4 libSystem.B.dylib 0x33bd724a_pthread_wqthread+258
5 libSystem.B.dylib 0x33bcf970启动线程+0
线程2:
0 libSystem.B.dylib 0x33b5d268马赫/平方米陷阱+20
1 libSystem.B.dylib 0x33b5f354马赫数msg+44
2 CoreFoundation 0x33a48648\uU CFRunLoopServiceMachPort+88
3 CoreFoundation 0x33a47ed2\uuu CFRunLoopRun+350
4 CoreFoundation 0x33a47c80 CFRunLoopRunSpecific+224
5 CoreFoundation 0x33a47b88 CFRunLoopRunInMode+52
6网络核心0x304df124运行网络线程(无效*)+332
7 libSystem.B.dylib 0x33bd6886_pthread_start+242
8 libSystem.B.dylib 0x33bcba88线程\u开始+0
线程3:
0 libSystem.B.dylib 0x33b5d268马赫/平方米陷阱+20
1 libSystem.B.dylib 0x33b5f354马赫数msg+44
2 CoreFoundation 0x33a48648\uU CFRunLoopServiceMachPort+88
3 CoreFoundation 0x33a47ed2\uuu CFRunLoopRun+350
4 CoreFoundation 0x33a47c80 CFRunLoopRunSpecific+224
5 CoreFoundation 0x33a47b88 CFRunLoopRunInMode+52
6基金会0x336465 F6+[ NSulLink(NSURLCounRealRealEnguly])RealSeleOrdLoC:+ 206
7基金会0x33624192-[NStHungEng] + 38
8基金会0x3361D242,NStRead Sym MnIsI+ 966
9 libSystem.B.dylib 0x33bd6886_pthread_start+242
10 libSystem.B.dylib 0x33bcba88线程\u开始+0
线程4:
0 libSystem.B.dylib 0x33b8168c选择$DARWIN_EXTSN+20
1 CoreFoundation 0x33a7f662\uuu CFSocketManager+582
2 libSystem.B.dylib 0x33bd6886_pthread_start+242
3 libSystem.B.dylib 0x33bcba88线程\u开始+0
线程5:
0 libSystem.B.dylib 0x33bd79e0 uu workq_ukernreturn+8
1 libSystem.B.dylib 0x33bd7364_pthread_wqthread+540
2 libSystem.B.dylib 0x33bcf970 start_wqthread+0
线程6:
0 libSystem.B.dylib 0x33bd79e0 uu workq_ukernreturn+8
1 libSystem.B.dylib 0x33bd7364_pthread_wqthread+540
2 libSystem.B.dylib 0x33bcf970 start_wqthread+0
未知线程崩溃,具有未知的味道:5,状态\u计数:1
更新:似乎有一个与此问题相关的更紧密的线程正在打开。 该错误与重定向http连接有关,包括异步和同步NRURL操作。也许,既然NSURLCONTION继承了基础框架,那么iPhone上也存在bug?


openradar网址:

我的调查显示,无论真正的原因是什么,您都会得到以下例外情况:

TMS[13544] has active assertions beyond permitted time: 
{(
    <SBProcessAssertion: 0x8cb6040> identifier: UIKitBackgroundCompletionTask process: TMS[13544] permittedBackgroundDuration: 600.000000 reason: finishTask owner pid:13544 preventSuspend  preventIdleSleep 
)}
TMS[13544]在允许的时间之外有活动断言:
{(
标识符:UIKitBackgroundCompletionTask进程:TMS[13544]permittedBackgroundDuration:600.000000原因:finishTask所有者pid:13544 preventSuspend preventIdleSleep
)}
如果代码未执行'endBackgroundTask:'方法

这意味着使用
beginBackgroundTaskWithExpirationHandler:“调用必须以
endBackgroundTask:”结束创建的每个后台任务。
仔细检查是否使用正确的任务id调用了“endBackgroundTask:”。

如果后台任务未及时结束,则会导致此异常。虽然我不能100%确定,但我相信发生这种崩溃是因为您传递到
beginBackgroundTaskWithExpirationHandler:
的块,如果连接尚未完成,系统希望结束您的后台任务,则会调用该块

UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    dispatch_async(dispatch_get_main_queue(), ^{
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        });
}];
-(UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^)(void))处理程序的文档说明了
处理程序
:“您应该使用此处理程序清理并标记后台任务的结束”

由于
dispatch\u async
调用,调用此块时实际上并没有结束任务。
UIApplication *app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier tid = bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    [app endBackgroundTask:tid];
    dispatch_async(dispatch_get_main_queue(), ^{
            bgTask = UIBackgroundTaskInvalid;
        });
}];
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];