添加NSOperation崩溃

时间:2015-12-07 03:37:53

标签: ios crash pthreads nsoperation nsoperationqueue

设置:在我的应用中,我有一个包含大量注释的地图。这些注释以群集形式显示,其中数据从服务器加载。

要创建群集,请使用具有以下属性的USE_CREDENTIALS查询注释数据库:

NSOperationQueue

要添加操作,请执行以下操作:

qualityOfService = NSQualityOfServiceUtility;
maxConcurrentOperationCount = 1;

问题:当用户四处移动,并且来自服务器的信息返回时,我得到以下崩溃- (void)updateAfterServerReturns { [_q cancelAllOperations]; NSBlockOperation* operation = [NSBlockOperation blockOperationWithBlock: ^{ __weak typeof(self) weakSelf = self; [weakSelf myOperation] }]; if(operation) { @autoreleasepool { [_q addOperation:operation]; } } } - (void)myOperation { //retrieve data, then update map __weak typeof(self) weakSelf = self; [weakSelf updateMap]; } - (void)updateMap { __weak typeof(self) weakSelf = self; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //update }]; } ,这会导致一切严重停顿。事故并不是每次都会发生,但往往是不可接受的。

有人有什么建议吗?谢谢!

崩溃详情

在日志中:

enqueued from com.apple.main-thread(Thread 1)

单步执行发生崩溃的线程,这些是:

2015-12-06 23:34:31.215 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x184450f48 0x199003f80 0x18433a754 0x100224af8 0x100119c18 0x100119ca4 0x100119c84 0x100119cd0 0x100119c64 0x100119cd0 0x100119ca4 0x100119cd0 0x100119c64 0x100119c84 0x100223b9c 0x1003d50c0 0x185378374 0x1852cb1a0 0x1852bb3e8 0x18537a768 0x100e2dc68 0x100e3a780 0x100e31958 0x100e3c898 0x100e3c590 0x199a35470 0x199a35020)
libc++abi.dylib: terminating with uncaught exception of type NSException
(Recorded Frame) 

更多详情:

libsystem_kernel.dylib`__pthread_kill:
0x19996f138 <+0>:  movz   x16, #0x148
0x19996f13c <+4>:  svc    #0x80
->  0x19996f140 <+8>:  b.lo   0x19996f158               ; <+32>
 0x19996f144 <+12>: stp    x29, x30, [sp, #-16]!
    0x19996f148 <+16>: mov    x29, sp
    0x19996f14c <+20>: bl     0x199956140               ; cerror_nocancel
    0x19996f150 <+24>: mov    sp, x29
    0x19996f154 <+28>: ldp    x29, x30, [sp], #16
    0x19996f158 <+32>: ret    

libdispatch.dylib`_dispatch_call_block_and_release:
    0x100e2dc90 <+0>:  stp    x20, x19, [sp, #-32]!
    0x100e2dc94 <+4>:  stp    x29, x30, [sp, #16]
    0x100e2dc98 <+8>:  add    x29, sp, #16
    0x100e2dc9c <+12>: mov    x19, x0
    0x100e2dca0 <+16>: ldr    x8, [x19, #16]
    0x100e2dca4 <+20>: blr    x8
->  0x100e2dca8 <+24>: mov    x0, x19
    0x100e2dcac <+28>: ldp    x29, x30, [sp, #16]
    0x100e2dcb0 <+32>: ldp    x20, x19, [sp], #32
    0x100e2dcb4 <+36>: b      0x100e59fec               ; symbol stub for: _Block_release

1 个答案:

答案 0 :(得分:0)

首先确保在插入任何对象之前初始化该数组。

例如,如果我们谈论的是NSMutableArray,那么

NSMutableArray *arr = [[NSMutableArray alloc] init];

其次,确保没有插入任何值;在插入之前做出一个条件:

if(object) {
    // Insert to array
} else { 
    // Don't insert and investigate why it returns nil if needed
}

通过这样做,你很可能会摆脱崩溃。

然而,如果它仍然被复制,我建议使用异常断点调试它并找到问题的具体来源,请点击此链接以了解如何执行此操作: Exception Breakpoints