Search code examples
iosexceptionbreakpointsnsmanagedobjectcontext

iOS - Managed Object Context Exception


When I enable exception breakpoints I got an exception for this code below.

I don't know why, but each time I launch the App the breakpoint triggers.

Is there something I don't understand about exception breakpoints or should I investigate on the managed object context ?

EDIT :

Code example :

-(void)parseJson
{
    _resultArray = [[NSMutableArray alloc]init];
    BOOL success = YES;
    NSError *saveError = nil;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeCoreDataChangesToMainContext:) name:NSManagedObjectContextDidSaveNotification object:self.managedObjectContext];

    [self doCustomActionBeforeParsing];
    for (NSDictionary* entries in _jsonArray) {
        id aMTLObject = [[MTLDatabaseManager sharedInstance] mantleObjectModel:_modelClass anItem:entries];
        [self doCustomActionBeforeCoreDataInsertionOfMantleObject:aMTLObject];
        [[MTLDatabaseManager sharedInstance] insertMantleObjectInContext:aMTLObject aManagedObjectContext:self.managedObjectContext];
        if ([self isCancelled])
            break;
    }

    if (![self isCancelled] && [self.managedObjectContext hasChanges]) {
        [self.managedObjectContext performBlockAndWait:^{
            success = [self.managedObjectContext save:&saveError];
        }];
    }

    if (![self isCancelled]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (success) {
                [self.delegate parserDidFinishParsingOperation:self fromUrl:self.dataUrl];
            } else {
                [self.delegate parserOperation:self didFailParsingOperationWithError:saveError fromUrl:self.dataUrl];
            }
        });
    }

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:self.managedObjectContext];
}

Is this that way you use the performAndWait block ?

The exception breakpoint triggers at this line :

success = [self.managedObjectContext save:&saveError];

I saw in this post that we can ignore certain exceptions. ignore exception

Is it a good practice to ignore CoreData exceptions ? (I guess not).


Solution

  • Try wrapping

    self.managedObjectContext.performAndWait {
        //all your processing managed objects
        //finally once done 
        try! self.managedObjectContext.save()
    }
    

    What might be happening,

    You might be accessing the managedObjects or might be calling managedObjectContext save on a thread which might belong to a different queue than the one to which your self.managedObjectContext is associated.

    iOS 5 onwards, when you make use of MainQueue concurrency or ProvateQueue Concurrency model, you can make use of ManagedObjectContext's perform or performAndWait to ensure managed object context is accessed only by the thread belonging to the same queue as the one with which managedObject Context is associated.