RestKit在RKManagedObjectRequestOperation objc_msgSend期间崩溃

时间:2014-04-25 18:52:58

标签: ios objective-c core-data restkit nsfetchrequest

我有一个函数,我们只需将其称为“更新函数”,它将RKManagedObjectRequestOperation发送到我的服务器以检索一些对象。我将此操作配置为自动saveToPersistentStore,然后启动它。在RKManagedObjectRequestOperation的'success'完成处理程序中,我调用一个函数来执行NSFetchRequest以获取满足特定条件的更新对象列表,并在完成块中有新数据时传回该数组和BOOL标志。 / p>

在“更新函数”的完成处理程序中,我调用一个名为getUpdatedList的函数,它执行NSFetchRequest请求,如下所示:

    NSUInteger now = [[NSDate date] timeIntervalSince1970];

    NSArray *predicates = @[
                            [NSPredicate predicateWithFormat:@"endAt > %d", now],
                            [NSPredicate predicateWithFormat:@"deleted != %@", @YES]
                            ];

    NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; //(REMOVED: _objectManager.managedObjectStore.persistentStoreManagedObjectContext;)

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
    [fetchRequest setPredicate:compoundPredicate];

    NSError *error = nil;

    NSArray *fetchedRecords = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

    if (sortDescriptor) {
        fetchedRecords = [fetchedRecords sortedArrayUsingDescriptors:@[sortDescriptor]];
    }

    DDLogInfo(@"Retrieving list of %@ objects from local database - got %d results.", entityName, fetchedRecords.count);

    return fetchedRecords;

- (NSManagedObjectContext *)managedObjectContext {
    if (_globalContext != nil) {
        return _globalContext;
    }

    NSPersistentStoreCoordinator *coordinator = _objectManager.managedObjectStore.persistentStoreCoordinator;
    if (coordinator != nil) {
        _globalContext = [[NSManagedObjectContext alloc] init];
        [_globalContext setPersistentStoreCoordinator:coordinator];
    }
    return _globalContext;
}

现在,我从来没有亲自看过这次崩溃,所以我不确切知道发生了什么,但是在我的日志中,我得到的结果是“从本地数据库中检索%@对象的列表 - 得到了x结果。“,然后日志将重新开始,这会在应用程序崩溃时发生。在这个RKManagedObjectRequestOperation中并且返回更新列表的某个地方,应用程序崩溃了1/1000次......

以下是我从Crashlytics获得的堆栈跟踪之一。有许多函数调用'Update Function',一些来自后台,一些来自前台,但所有堆栈跟踪都引用了这个'Update Function',你可以在下面第9行看到它(updateAndFetchInfoWithCompletionHandler): / p>

    Thread : com.apple.uikit.backgroundTaskAssertionQueue
0  libsystem_kernel.dylib         0x38911a50 mach_msg_trap + 20
1  libsystem_kernel.dylib         0x3891184d mach_msg + 40
2  SpringBoardServices            0x34d2b42d SBGetBackgroundTimeRemaining + 52
3  SpringBoardServices            0x34d29461 SBSGetBackgroundTimeRemaining + 36
4  UIKit                          0x30685b23 __40-[UIApplication backgroundTimeRemaining]_block_invoke + 10
5  libdispatch.dylib              0x38858d3f _dispatch_client_callout + 22
6  libdispatch.dylib              0x3885d6c3 _dispatch_barrier_sync_f_invoke + 26
7  UIKit                          0x30685a97 -[UIApplication backgroundTimeRemaining] + 182
8  <redacted>                     0x002345df __46-[LocationManager handleExitedRegion]_block_invoke_2 (LocationManager.m:262)
9  <redacted>                     0x002451e3 __57-[DataManager updateAndFetchInfoWithCompletionHandler:]_block_invoke (DataManager.m:719)
10 <redacted>                     0x00340d0d __66-[RKObjectRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke229 (RKObjectRequestOperation.m:506)
11 libdispatch.dylib              0x38858d53 _dispatch_call_block_and_release + 10
12 libdispatch.dylib              0x38858d3f _dispatch_client_callout + 22
13 libdispatch.dylib              0x3885b6c3 _dispatch_main_queue_callback_4CF + 278
14 CoreFoundation                 0x2dba3681 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
15 CoreFoundation                 0x2dba1f4d __CFRunLoopRun + 1308
16 CoreFoundation                 0x2db0c769 CFRunLoopRunSpecific + 524
17 CoreFoundation                 0x2db0c54b CFRunLoopRunInMode + 106
18 GraphicsServices               0x32a796d3 GSEventRunModal + 138
19 UIKit                          0x3046b891 UIApplicationMain + 1136
20 <redacted>                     0x000a3197 main (main.m:18)

这里有什么明显的东西我错过了导致所有这些崩溃吗?

编辑:更改上述代码以反映评论后的不同托管对象上下文。

1 个答案:

答案 0 :(得分:1)

你不应该使用persistentStoreManagedObjectContext。如果此代码在主线程上运行,那么您必须使用主队列上下文。如果在任意后台线程上运行,则必须专门为该调用创建新上下文。

上下文是特定于线程的,您必须尊重您创建的每个上下文的线程所有权。

在后台线程上运行时,使用newChildManagedObjectContextWithConcurrencyType:tracksChanges:创建新上下文。这样可以保持上下文所需的线程限制。