在快速连续发出多个请求时,使用RestKit崩溃

时间:2012-07-28 10:05:57

标签: objective-c ios cocoa-touch thread-safety restkit

我有一个带有两个按钮的按钮,可以从RestKit的Web服务器开始下载一些数据。

现在,如果用户快速连续重复点击两个按钮,我的应用程序崩溃并生成下面的崩溃日志。

我这样提出了我的要求:

-(void)loadDataAtPath:(NSString *)path completion:(ResultArrayBlock)completionBlock failed:(ResultFailedBlock)failedBlock
{
    RKObjectMapping *groupMapping = [Group mapping];

    [self.objectManager.mappingProvider setMapping:groupMapping forKeyPath:@"groups.group"];
    [self.objectManager.mappingProvider setObjectMapping:groupMapping forKeyPath:path];  

    [self.objectManager loadObjectsAtResourcePath:path usingBlock:^(RKObjectLoader *loader) {
        loader.onDidLoadObjects = ^(NSArray *array){

            // Do the reverse mapping of the group
            for (Group *c in array) {
                for(Person *p in c.persons){
                    p.group = c;
                }
            }

            completionBlock(array);

        };
        loader.onDidFailWithError = failedBlock;
    }];
}

我首先想到的问题是for-loop我在做数据的映射后设置,但是即使在评论for循环时问题仍然存在。

奇怪的是,即使我打开 Network Link Conditioner.prefpane

,模拟器中也不会出现此问题

崩溃

当我在设备上调试它时,我在控制台上得到以下内容。

[PLCrashReport] Terminated stack walking early: Corrupted frame
[PLCrashReport] Terminated stack walking early: Corrupted frame

崩溃日志如下所示:

Application Specific Information:
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFDictionary: 0x3c14a0> was mutated while being enumerated.'

Last Exception Backtrace:
0   CoreFoundation                      0x3734e88f __exceptionPreprocess + 162
1   libobjc.A.dylib                     0x35053259 objc_exception_throw + 32
2   CoreFoundation                      0x3734e3b3 __NSFastEnumerationMutationHandler + 162
3   MyApp                               0x0008f5bf -[RKObjectMapper performKeyPathMappingUsingMappingDictionary:] + 407
4   MyApp                               0x0008fa45 -[RKObjectMapper performMapping] + 673
5   MyApp                               0x0008ac7d -[RKObjectLoader mapResponseWithMappingProvider:toObject:inContext:error:] + 1045
6   MyApp                               0x0008b04f -[RKObjectLoader performMapping:] + 575
7   MyApp                               0x0008b22b __47-[RKObjectLoader performMappingInDispatchQueue]_block_invoke_0 + 247
8   libdispatch.dylib                   0x3046ec59 _dispatch_call_block_and_release + 12
9   libdispatch.dylib                   0x30479cab _dispatch_queue_drain + 274
10  libdispatch.dylib                   0x30479b19 _dispatch_queue_invoke$VARIANT$up + 36
11  libdispatch.dylib                   0x3047a78b _dispatch_worker_thread2 + 214
12  libsystem_c.dylib                   0x33bbddfb _pthread_wqthread + 294
13  libsystem_c.dylib                   0x33bbdcd0 start_wqthread + 8

1 个答案:

答案 0 :(得分:3)

正如@PhillipMills建议的那样,当你查看performKeyPathMappingUsingMappingDictionary时,你可以很容易地看到答案。您的问题是使用以下行重复设置映射:

[self.objectManager.mappingProvider setMapping:groupMapping forKeyPath:@"groups.group"];
[self.objectManager.mappingProvider setObjectMapping:groupMapping forKeyPath:path];  

如果在响应映射时调用此行,则会触发错误,因为您正在更改它尝试快速枚举的相同字典。

通常情况下,您可以在初始配置中的某处设置映射,而不是每次都这样。