我正在使用 MagicalRecord 并在此方法中为实体导入数据:
+(void)importEnduserOnBackgroundFromResponse:(id)responseObject
success:(void (^)(QNEnduser *provider))success
andFailure:(void (^)(NSError *error))failure
{
__block NSManagedObjectID *enduserID;
__block NSError *importError;
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
QNEnduser *enduser = [QNEnduser MR_importFromObject:enduserDict inContext:localContext];
[localContext obtainPermanentIDsForObjects:localContext.insertedObjects.allObjects error:&importError];
enduserID = enduser.objectID;
} completion:^(BOOL successFlag, NSError *saveError) {
// Check for errors (importError and saveError), if no errors import the entity in the main context and call the success block
QNEnduser *newEnduser = (QNEnduser*)[[NSManagedObjectContext MR_defaultContext] objectWithID:enduserID];
}];
}
请注意,我在完成块中删除了一些代码,因为它对于这个问题并不重要。
在Xcode 5中,此代码有效。但是在Xcode 6 Beta 6中,它会在[localContext obtainPermanentIDsForObjects:localContext.insertedObjects.allObjects error:&importError];
我发现了类似的问题(在调用 gainPermanentIDsForObjects 时出现线程错误,核心数据:子上下文是否会为新插入的对象获得永久对象 ID?),但是它们较旧,并且它们的解决方案在我的情况下不起作用,如您所见,我已经尝试为所有插入的对象获取永久 ID(尽管无论如何都没有关系,没有其他插入的对象)。
有没有其他人遇到过这个问题,它是 beta 6 中的一个已知 Bug,是否有已知的解决方法?还是毕竟是我的错?
编辑
不知何故我无法得到异常——我在 Xcode 中设置了一个异常断点,但是当我@try/@catch
在坏行周围放置一个异常断点时,什么都没有被捕获。
当在异常断点处停止时,这是回溯:
* thread #4: tid = 0x2f2a0, 0x0000000103f19973 libobjc.A.dylib`objc_exception_throw, queue = 'NSManagedObjectContext Queue', stop reason = breakpoint 1.3
frame #0: 0x0000000103f19973 libobjc.A.dylib`objc_exception_throw
frame #1: 0x0000000102333dd2 CoreData`-[NSPersistentStoreCoordinator(_NSInternalMethods) obtainPermanentIDsForObjects:error:] + 1458
frame #2: 0x00000001023aa16d CoreData`__99-[NSManagedObjectContext(_NestedContextSupport) _parentObtainPermanentIDsForObjects:context:error:]_block_invoke + 1037
frame #3: 0x0000000104f5772d libdispatch.dylib`_dispatch_client_callout + 8
frame #4: 0x0000000104f465d0 libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 57
frame #5: 0x000000010233df92 CoreData`_perform + 114
frame #6: 0x00000001023a9bde CoreData`-[NSManagedObjectContext(_NestedContextSupport) _parentObtainPermanentIDsForObjects:context:error:] + 446
frame #7: 0x0000000102333592 CoreData`-[NSManagedObjectContext obtainPermanentIDsForObjects:error:] + 498
* frame #8: 0x000000010206fc28 QonnectAPIPod`__75+[QNImportHelper importEnduserOnBackgroundFromResponse:success:andFailure:]_block_invoke(.block_descriptor=<unavailable>, localContext=0x00007fd078dd8d60) + 568 at QNImportHelper.m:129
frame #9: 0x0000000102142abd QonnectAPIPod`__51+[MagicalRecord(.block_descriptor=0x00007fd078d026b0) saveWithBlock:completion:]_block_invoke + 25 at MagicalRecord+Actions.m:28
frame #10: 0x00000001023a461e CoreData`developerSubmittedBlockToNSManagedObjectContextPerform_privateasync + 78
frame #11: 0x0000000104f5772d libdispatch.dylib`_dispatch_client_callout + 8
frame #12: 0x0000000104f46dcf libdispatch.dylib`_dispatch_queue_drain + 481
frame #13: 0x0000000104f46b94 libdispatch.dylib`_dispatch_queue_invoke + 112
frame #14: 0x0000000104f479fc libdispatch.dylib`_dispatch_root_queue_drain + 81
frame #15: 0x0000000104f47d12 libdispatch.dylib`_dispatch_worker_thread2 + 40
frame #16: 0x00000001052ebef8 libsystem_pthread.dylib`_pthread_wqthread + 314
frame #17: 0x00000001052eefb9 libsystem_pthread.dylib`start_wqthread + 13