Question

I am quite puzzled by a crash that I get whilst saving my main 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");
    }
}];
}

Here's the exception and stacktrace:

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

The saveMainMOCmethod is invoked from the saveLocalMOCmethod which lies hereafter:

-(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];
}];
}

NB: it crashes the same if I remove the obtainPermanentIDsForObjectsfrom both procedures.

Here is also the _localMOCDidSave observer.

- (void)_localMOCDidSave:(NSNotification *)notification {
if (_mainMOC) {
    [_mainMOC performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
                                   withObject:notification waitUntilDone:YES];
}
}

I am really confused by all of this, what do I do wrong?

The main and local MOCS are initialized that way:

    _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];

the saveMOC is created that way:

        _saveMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        [_saveMOC setPersistentStoreCoordinator:coordinator];

The error usually comes during the initial load of several hundreds of Posts records.

Was it helpful?

Solution

(Partially) fixed here:

This is how I solved it:

    [_mocInMemoryForDynamicInformation.parentContext obtainPermanentIDsForObjects:@[session] error:&error];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top