gpt4 book ai didi

ios - 添加 NSOperation 崩溃

转载 作者:行者123 更新时间:2023-12-01 20:12:10 25 4
gpt4 key购买 nike

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

要创建集群,请使用具有以下属性的 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

更多详细信息:

Foundation`__NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__:
0x185378364 <+0>: stp x29, x30, [sp, #-16]!
0x185378368 <+4>: mov x29, sp
0x18537836c <+8>: ldr x8, [x0, #16]
0x185378370 <+12>: blr x8
-> 0x185378374 <+16>: ldp x29, x30, [sp], #16
0x185378378 <+20>: ret

最佳答案

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

例如,如果我们正在谈论 NSMutableArray,那么

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

其次,确保没有插入 nil 值;插入之前先设定一个条件:

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

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

但是,如果它仍然被重现,我建议使用异常断点对其进行调试并找到问题的具体来源,请点击此链接了解如何执行此操作: Exception Breakpoints

关于ios - 添加 NSOperation 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com