ArtSabintsev
ArtSabintsev

Reputation: 5200

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?

Upvotes: 8

Views: 3082

Answers (2)

ArtSabintsev
ArtSabintsev

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

Related Questions