我对在保存主要MOC时遇到的崩溃感到非常困惑:
-(void) saveMainMOC
{
[_mainMOC performBlockAndWait:^{
NSError *errMain=nil;
NSMutableArray *objs = [NSMutableArray array];
for (NSManagedObject *obj in [[[_mainMOC insertedObjects] allObjects] arrayByAddingObjectsFromArray:[[_mainMOC updatedObjects] allObjects]]) {
if (obj) {
[objs addObject:obj];
}
}
if (![_mainMOC obtainPermanentIDsForObjects:objs error:&errMain]) {
NSLog(@"MainViewController::saveMainMOC.obPeID");
}
if (![_mainMOC save:&errMain]) { // THE SIGABRT OCCURS HERE
NSLog(@"MainViewController::saveMainMOC.saveMain");
}
}];
}
这是例外和堆栈跟踪:
2014-05-03 17:52:27.317 uRSS[4060:60b] *** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x9c31690 <x-coredata://9092A9F4-DAE0-4413-AADF-E137FBB3A860/Post/p1128>''
*** First throw call stack:
(
0 CoreFoundation 0x028fd1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01faf8e5 objc_exception_throw + 44
2 CoreData 0x01c80beb _PFFaultHandlerLookupRow + 2715
3 CoreData 0x01c80147 -[NSFaultHandler fulfillFault:withContext:forIndex:] + 39
4 CoreData 0x01c7fd23 _PF_FulfillDeferredFault + 259
5 CoreData 0x01c8c0a9 _PF_Handler_WillAccess_Property + 57
6 CoreData 0x01c8beda _PF_ManagedObject_WillChangeValueForKeyIndex + 74
7 CoreData 0x01c8f4c7 _sharedIMPL_setvfk_core + 151
8 CoreData 0x01cc0d06 -[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) _setGenericValue:forKey:withIndex:flags:] + 54
saveMainMOC
方法是从以下的saveLocalMOC
方法调用的:
-(void)saveLocalMOC
{
[_localMOC performBlockAndWait:^{
NSError *errLoc=nil;
NSMutableArray *objs = [NSMutableArray array];
for (NSManagedObject *obj in [[[_localMOC insertedObjects] allObjects] arrayByAddingObjectsFromArray:[[_mainMOC updatedObjects] allObjects]]) {
if (obj) {
[objs addObject:obj];
}
}
if (![_localMOC obtainPermanentIDsForObjects:objs error:&errLoc]) {
NSLog(@"MainViewController::saveLocalMOC.obPeID");
}
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(_localMOCDidSave:)
name:NSManagedObjectContextDidSaveNotification object:_localMOC];
if (![_localMOC save:&errLoc]) {
NSLog(@"MainViewController::saveLocalMOC.saveLocal");
}
[nc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:_localMOC];
[self saveMainMOC];
}];
}
注意:如果我从两个程序中删除obtainPermanentIDsForObjects
,它会崩溃。
这里也是_localMOCDidSave
观察员。
- (void)_localMOCDidSave:(NSNotification *)notification {
if (_mainMOC) {
[_mainMOC performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification waitUntilDone:YES];
}
}
我真的很困惑所有这些,我做错了什么?
主要和本地MOCS以这样的方式初始化:
_mainMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_mainMOC setParentContext:_saveMOC];
[_mainMOC setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
self.localMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[self.localMOC setParentContext:self.mainMOC];
[self.localMOC setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
以这种方式创建saveMOC:
_saveMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_saveMOC setPersistentStoreCoordinator:coordinator];
错误通常发生在数百个帖子记录的初始加载过程中。
答案 0 :(得分:0)
(部分)已修复here:
这就是我解决它的方法:
[_mocInMemoryForDynamicInformation.parentContext obtainPermanentIDsForObjects:@[session] error:&error];