objective-cmultithreadingcore-datansmanagedobjectcontextnspersistentdocument

Can concurrence type be changed of NSPersistentDocument's managedObjectContext?


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


Solution

  • 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;
    }