使用NSManagedObjectContext刷新

时间:2016-04-10 18:55:27

标签: ios core-data nsmanagedobject nsmanagedobjectcontext crash-reports

我偶尔会收到用户的崩溃报告(我自己无法复制);它与NSManagedObjectContext refreshObject有关。崩溃消息是

  

" NSManagedObjectContext无法刷新其他上下文中的对象"

崩溃的代码在这里:

dispatch_async(self.filterMainQueue, ^{
    NSArray *items = [Person getAllNonPrivatePersonsWithContext: self.backgroundContextImage];

    if (items.count) {

        for (Person *person in items) {

                [self.backgroundContextImage performBlockAndWait: ^{
                    [person loadContactReferenceAndImage];

                    // crashes here
                    [self.backgroundContextImage refreshObject: person mergeChanges:NO];
                }];
        }               
    }
});

+ (NSArray *) getAllNonPrivatePersonsWithContext: (NSManagedObjectContext *) context{

    __block NSArray *items = nil;

    [context performBlockAndWait: ^{
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext: context];
        [fetchRequest setEntity:entity];
        // Set the batch size to a suitable number.
        //[fetchRequest setFetchBatchSize: 500];
        [fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"isContactPrivate == FALSE"]];
        [fetchRequest setReturnsObjectsAsFaults: FALSE];
        [fetchRequest setIncludesPendingChanges: FALSE];

        NSError *error = nil;
        items = [context executeFetchRequest:fetchRequest error:&error];

    }];
    return items;
}

我不太清楚为什么会崩溃。 backgroundContextImage是使用NSPrivateQueueConcurrencyType创建的,我确保在使用该上下文获取和访问托管对象时使用performBlockAndWaitfilterMainQueue是一个串行队列,用于帮助在后台线程而不是主线程中完成此工作。

崩溃报告如下所示:

Exception Type:  SIGABRT
Exception Codes: #0 at 0x197fb7140
Crashed Thread:  3

Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An NSManagedObjectContext cannot refresh objects in other contexts.'

Last Exception Backtrace:
0   CoreFoundation                       0x0000000182a98f48 __exceptionPreprocess + 124
1   libobjc.A.dylib                      0x000000019764bf80 objc_exception_throw + 52
2   CoreData                             0x0000000182786898 -[NSManagedObjectContext refreshObject:mergeChanges:] + 1320
3   CJournal                             0x00000001001729c4 __75-[ContactsSyncController loadContactImagesAndLinksWithPermissionWithBlock:]_block_invoke_2 (ContactsSyncController.m:102)
4   CoreData                             0x00000001827dc900 developerSubmittedBlockToNSManagedObjectContextPerform + 192
5   libdispatch.dylib                    0x0000000197e696a8 _dispatch_client_callout + 12
6   libdispatch.dylib                    0x0000000197e74954 _dispatch_barrier_sync_f_invoke + 96
7   CoreData                             0x00000001827dc7e8 -[NSManagedObjectContext performBlockAndWait:] + 248
8   CJournal                             0x00000001001728bc __75-[ContactsSyncController loadContactImagesAndLinksWithPermissionWithBlock:]_block_invoke (ContactsSyncController.m:97)
9   libdispatch.dylib                    0x0000000197e696e8 _dispatch_call_block_and_release + 20
10  libdispatch.dylib                    0x0000000197e696a8 _dispatch_client_callout + 12
11  libdispatch.dylib                    0x0000000197e756ec _dispatch_queue_drain + 860
12  libdispatch.dylib                    0x0000000197e6d1ac _dispatch_queue_invoke + 460
13  libdispatch.dylib                    0x0000000197e696a8 _dispatch_client_callout + 12
14  libdispatch.dylib                    0x0000000197e77b40 _dispatch_root_queue_drain + 2136
15  libdispatch.dylib                    0x0000000197e772dc _dispatch_worker_thread3 + 108
16  libsystem_pthread.dylib              0x000000019807d470 _pthread_wqthread + 1088
17  libsystem_pthread.dylib              0x000000019807d020 start_wqthread + 0

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

问题在于,在特殊情况下,应用程序中的其他内容将替换persistentStore并重置managedObjectContext,即使loadContactReferenceAndImage和refreshObject调用都在performBlockAndWait中。我想出的解决方案是确认managedObjectContext是否相同,然后才刷新对象:

       [self.backgroundContextImage performBlockAndWait: ^{
              [person loadContactReferenceAndImage];

              // MOC could have been updated by another thread
              if (person.managedObjectContext == self.backgroundContextImage) {
                  [self.backgroundContextImage refreshObject: person mergeChanges:NO];
              }

       }];