Can concurrence type be changed of NSPersistentDocument's managedObjectContext?

138 views Asked by At

I need the concurrence type of NSPersistentDocument's managedObjectContext to be NSMainQueueConcurrencyType because I need contexts in other threads.

2

There are 2 answers

0
93sauu On BEST ANSWER

Yes, you were right. Overriding manageObjectContext you can modify concurrency type. Maybe, I have some mistakes.

 - (NSManagedObjectContext *)managedObjectContext {
    __strong static NSManagedObjectContext *myManagedObjectContext = nil;

    if (myManagedObjectContext == nil) {
        myManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

        NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
        [myManagedObjectContext setPersistentStoreCoordinator:psc];
    }

    return myManagedObjectContext;
}
0
Tom Harrington On

The documentation for NSPersistentDocument says this about the managedObjectContext property:

If you want to customize the creation of the persistence stack, reimplement this property in your custom subclass and use your implementation to create the appropriate objects.

That seems pretty clear. Override this property and use whatever concurrency type you want. Have you tried it? Did it not work?