ios - __NSArrayM objectAtIndex 崩溃,但我进行了验证

标签 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] 时,出现异常:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

但是从我的崩溃报告中我看到:

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

两者是相同的还是因为多线程而发生问题?

最佳答案

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

根据类中其余部分发生的情况,我将使用 cachedRequests 对象作为同步 @synchronized (cachedRequests) 的 token 。取决于您是否还有其他东西想要与同一个锁同步?

您还需要使用相同的 token 向操作此数组的任何其他代码添加相同的锁,以同步对其的所有访问。

关于ios - __NSArrayM objectAtIndex 崩溃,但我进行了验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43386469/

相关文章:

ios - 无法检索 firebase 用户的消息

iphone - 在后台应用程序时重新启动没有用户界面的 iOS 应用程序?

java - RxJava flatMap 和背压奇怪的行为

ios - 如何增加UIButtonTypeInfoLight的大小

ios - 在 PopOverViewController 中显示 UISplitviewController

ios - dyld : Library not loaded: @rpath/Stripe. 框架/Stripe

multithreading - 在单独的线程中运行 VCL

c# - 顶级工作方法中的异步模式

java - 如何在线程迭代之前和之后打印一些消息?

java - 检测 EC2 实例何时关闭(Java SDK)