Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMergeByPropertyObjectTrumpMergePolicy vs. NSMergeByPropertyStoreTrumpMergePolicy

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?

like image 615
ArtSabintsev Avatar asked Oct 29 '12 22:10

ArtSabintsev


1 Answers

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];

}
like image 164
ArtSabintsev Avatar answered Sep 22 '22 22:09

ArtSabintsev