崩溃__NSArrayM objectAtIndex但我做了验证

时间:2017-04-13 07:49:27

标签: ios multithreading

我遇到了这个崩溃:

Last Exception Backtrace:
0   CoreFoundation                  0x1838551b8 __exceptionPreprocess + 124 (NSException.m:165)
1   libobjc.A.dylib                 0x18228c55c objc_exception_throw + 56 (objc-exception.mm:521)
2   CoreFoundation                  0x18373071c -[__NSArrayM objectAtIndex:] + 228 (NSArray.m:389)
3   living                          0x100c0d89c -[RequestCache sendEvents] + 76
4   libdispatch.dylib               0x1826de1bc _dispatch_client_callout + 16 (object.m:455)
5   libdispatch.dylib               0x1826eaf94 _dispatch_continuation_pop + 576 (inline_internal.h:2424)
6   libdispatch.dylib               0x1826f7634 _dispatch_source_latch_and_call + 204 (source.c:594)
7   libdispatch.dylib               0x1826e0160 _dispatch_source_invoke + 820 (source.c:897)
8   libdispatch.dylib               0x1826e2bbc _dispatch_main_queue_callback_4CF + 572 (inline_internal.h:2461)
9   CoreFoundation                  0x183802810 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 (CFRunLoop.c:1793)
10  CoreFoundation                  0x1838003fc __CFRunLoopRun + 1660 (CFRunLoop.c:3004)
11  CoreFoundation                  0x18372e2b8 CFRunLoopRunSpecific + 444 (CFRunLoop.c:3113)
12  GraphicsServices                0x1851e2198 GSEventRunModal + 180 (GSEvent.c:2245)
13  UIKit                           0x1897757fc -[UIApplication _run] + 684 (UIApplication.m:2650)
14  UIKit                           0x189770534 UIApplicationMain + 208 (UIApplication.m:4092)
15  living                          0x10065fb2c main + 88 (main.m:15)
16  libdyld.dylib                   0x1827115b8 start + 4

以下代码发生崩溃:

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

   // ....

- (void) sendEvents {

    if (cachedRequests != nil && [cachedRequests count] == 0){
        return;
    }
    MyCacheData *requestData = [cachedRequests objectAtIndex:0]; // <- Crash happens here

    if (requestData != nil) {
        [cachedRequests removeObject:requestData];
    }    
}

我从不同的地方和不同的线程调用sendEvents,例如:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self sendEvents];
    });

或来自NSURLSessionDataTask

的回调

我是否需要使用@synchronized (self) {}锁定所有方法内容?

像:

- (void) sendEvents {
 @synchronized (self) {   
    if (cachedRequests != nil && [cachedRequests count] == 0){
        return;
    }
    MyCacheData *requestData = [cachedRequests objectAtIndex:0]; 

    if (requestData != nil) {
        [cachedRequests removeObject:requestData];
    }  
  }  
}

还是有其他更适合摆脱这种崩溃的方法?

我从生产中的版本中获得了iTunes Connect的这个例外

[编辑]

当我尝试为cachedRequests设置0长度并调用[cachedRequests objectAtIndex:0]时,我得到例外:

  

*由于未捕获的异常终止应用&#39; NSRangeException&#39;,原因:&#39; * - [__ NSArrayM objectAtIndex:]:索引0超出范围   空数组&#39;

然而,从我的崩溃报告中我看到:

-[__NSArrayM objectAtIndex:] + 228 (NSArray.m:389)

是否因为多线程而发生相同或问题?

2 个答案:

答案 0 :(得分:2)

我认为您的解决方案看起来很合理,这似乎是一个并发问题。

根据课程其余部分的内容,我会使用cachedRequests对象作为同步的标记@synchronized (cachedRequests)。取决于您是否还有其他想要与同一锁定同步的内容?

您还希望将相同的锁添加到操作此数组的任何其他代码,以同步对它的所有访问。

答案 1 :(得分:0)

sendEvents方法开始时,创建一个数组副本,因此:

NSArray *myCopiedArray = [cachedRequests copy];

然后,在if语句和objectAtIndex:中使用此副本。

但仍然使用行[cachedRequests removeObject:requestData];中的原始数组。

这样,您可以获取数组当前状态的快照,并使用完全相同的状态进行if比较并检索对象。