3
votes

After spending a lot of hours with this error I come here to ask if somebody have some information about this error. I load two entities, a Parks entity and a GuidedTour entity. A Paks can be relatet to many GuidedTours but when I try to save it, the error raises:

Error Domain=NSCocoaErrorDomain Code=133020 "(null)" UserInfo={conflictList=( "NSMergeConflict (0x17026afc0) for NSManagedObject (0x1740d94b0) with objectID '0xd000000000240002 ' with oldVersion = 0 and newVersion = and old cached row = {\n language = \"de_DE\";\n text = \"Apapapapapa"...}

I can't understand what is wrong, my Merge policy is: NSMergeByPropertyStoreTrumpMergePolicyType

And my code:

AppDelegate appDelegate = (AppDelegate)[[UIApplication sharedApplication] delegate]; self.context = [appDelegate managedObjectContext];

NSError *error = nil;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"GuidedTours" inManagedObjectContext:self.context ];
[fetchRequest setEntity:entity];

NSArray *fetchedObjects = [self.context  executeFetchRequest:fetchRequest error:&error];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"guided_tours_parks == NULL"];
fetchedObjects = [fetchedObjects filteredArrayUsingPredicate:predicate];

GuidedTours *tour = [fetchedObjects firstObject];



fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entityPark = [NSEntityDescription entityForName:@"Parks" inManagedObjectContext:self.context ];
[fetchRequest setEntity:entityPark];

fetchedObjects = [self.context  executeFetchRequest:fetchRequest error:&error];

predicate = [NSPredicate predicateWithFormat:@"name == %@",[jsonData objectForKey:@"Park ID"] ];
fetchedObjects = [fetchedObjects filteredArrayUsingPredicate:predicate];

Parks *park = [fetchedObjects firstObject];


[tour setGuided_tours_parks:park];

[park addParks_guided_toursObject:tour];



// Save the object to persistent store
if (![self.context  save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

Does somebody have any idea?

Thank you!

1

1 Answers

2
votes

I had this exact same error, and in my case it occurred on inserts after attempting to perform an NSBatchDeleteRequest. The problem was that after performing the NSBatchDeleteRequest, I was not calling the [self.context reset] method.

As per a WWDC 2015 talk, batch updates/deletions circumvent the context and directly modify the persistent store file. Thus, if you do not call reset, your context will contain deleted objects which can cause the above merge conflict you experienced.

https://developer.apple.com/videos/play/wwdc2015/220/