Reputation: 5200
In my multithreaded app, the main thread and one or more background threads may simultaneously access, fetch, and change information in my core data store. For each thread, I am creating a new NSManagedObjectContext
. However, each instance of NSManagedObjectContext
uses the same `NSPersistentStoreCoordinator' instance (stored elsewhere in a singleton).
My question is in regards to the merge policies of each instance of NSManagedObjectContext
. Is there an intrinsic benefit if I set one merge policy for background threads (NSMergeByPropertyStoreTrumpMergePolicy
) and another policy (NSMergeByPropertyObjectTrumpMergePolicy
) for the main thread?
In my NSMangagedObjectContext
getter, I have the following conditional:
if ( [NSThread isMainThread] ) {
[_context setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
} else {
[_context setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
}
Thank you.
Edit: Is it necessary? Should I just default to one policy over the other for both types of threads?
Upvotes: 8
Views: 3082
Reputation: 5200
I ended up going with this solution (well over a year ago), but as I received no answer to this question recently, I decided to put my own.
NSManagedObjectContext *context;
if ( [NSThread isMainThread] ) {
context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[context setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
} else {
context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[context setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
}
Upvotes: 3
Reputation: 1024
I think it is necessary if you are doing save from different contexts. See https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdChangeManagement.html#//apple_ref/doc/uid/TP30001201
Upvotes: 0