Search code examples
ioscore-datansmanagedobjectnsmanagedobjectcontextnsoperation

Getting "NSObjectInaccessibleException" error when updating an nsmanagedobject in secondary thread


In the app I'm developing I need to update objects I store locally with any changes that have occurred with the server's version of these object. To perform the update check I alloc an NSOperation subclass with its own NSManagedObjectContext and parse any updates that I receive from the server before merging these back into the main NSManageObjectContext.

The issue I'm having is that when I'm presenting an NSManagedObject in the UI that is then updated via the secondary NSManagedObjectContext, the app crashes with an:

'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for... exception

I don't get this issue when the update is completed and the app is not showing the NSManagedObject in the UI

This is the method used to created NSOperation's local NSManagedObjectContext:

- (NSManagedObjectContext *) localManagedObjectContext
{

    if (!_localManagedObjectContext)
    {
        _localManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
        [_localManagedObjectContext setParentContext:[CDAServiceManager managedObjectContext]];

        [_localManagedObjectContext setUndoManager:nil];
        [_localManagedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];

    }

    return _localManagedObjectContext;

}

Is there a strategy for updating a currently being used NSManagedObject from a secondary NSManagedObjectContext?


Solution

  • call refresh on your MO so that changes show up

    [ctx refreshObject:object mergeChanges:mergeChanges /*YES OR NO*/];