Вопрос

I need to save changes from my temp context to disk without blocking the Main UI thread. I am trying to do this like mentioned at the bottom of the article multi-context-coredata

Do the following:

NSManagedObjectContext *editContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
editContext.persistentStoreCoordinator = [NSManagedObjectContext defaultContext].persistentStoreCoordinator;

[NSManagedObjectContext defaultContext].parentContext = editContext;//defaultContext is my main CoreData context

But app crashes on on the string:

[NSManagedObjectContext defaultContext].parentContext = editContext

with the following:

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

What I am doing wrong?

Это было полезно?

Решение

The error message is pretty descriptive. You're trying to assign a parent context to a context that already has a persistent store coordinator. You can't do both. A managed object context may have either a persistent store coordinator or a parent context but not both. With nested contexts, only the root context has a persistent store coordinator of its own. Its child contexts (and their child contexts, etc) have parent contexts instead.

If you want to create a parent context, don't assign a value for the persistent store coordinator.

If you want to assign a value for the persistent store coordinator, don't set its parent context.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top