Cannot add parentContext to NSManagedObjectContext, context already has coordinator

I have a view where I get the saved Entity (Route *) from the main NSManagedObjectContext. I want to import this into tempContext. Following the examples of Marcus Zarra, I do this:

    NSManagedObjectContext *moc = _route.managedObjectContext;
    NSManagedObjectID *routeId = [_route objectID];
    NSPersistentStoreCoordinator *psc = moc.persistentStoreCoordinator;
    self.tempContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [self.tempContext setPersistentStoreCoordinator:psc];
    NSManagedObject *localRoute = [self.tempContext objectWithID:routeId];
    [localRoute moToDictionary:localRoute];
    self.tempContext.parentContext = moc; // crashes here

Everything is fine until I try to install parentContextmine tempContexton the main MOC. I get an error message:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Context already has a coordinator;  cannot replace.'

I understand that this tells me that I can’t change persistentStoreCoordinator. However, I'm not sure why this tells me this. When I set a breakpoint, it tempContextis in a different memory than the main moc. It self.tempContext.parentContextis also zero. So I would think that if it is zero, I can set the nil parameter to moc, but it will work. Any suggestions? Thanks in advance!

+4
1

  • , MOC ,
  • MOC,

.

+6

All Articles