IOS7 NSMergeConflict in one file

I'm having trouble simply writing data in a single-threaded application after two rows have been reordered.

I managed to simplify the coding to reproduce the error, and would appreciate a second opinion if someone else tries to do this.

This is a health check, because I am suspicious of the basic data issue introduced in iOS 7, since it works fine in iOS 6.

First, start a new project with basic data and create this model ...

simple core data model

The current attribute is an optional boolean. A one-to-many relationship is an ordered relationship that creates an NSOrderedDataset.

Now add a couple of buttons to the application; the first to create data (project and two related "drawings"), and the second to replace two drawings, then set the property within the first drawing.

Here is the code ...

-(IBAction)onTestButton:(id)sender { id delegate = [[UIApplication sharedApplication]delegate]; NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext]; self.project = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:managedObjectContext]; Drawing *drawing1 = [NSEntityDescription insertNewObjectForEntityForName:@"Drawing" inManagedObjectContext:managedObjectContext]; Drawing *drawing2 = [NSEntityDescription insertNewObjectForEntityForName:@"Drawing" inManagedObjectContext:managedObjectContext]; NSMutableOrderedSet* tempSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.project.drawings]; [tempSet addObject:drawing1]; [tempSet addObject:drawing2]; self.project.drawings = tempSet; [self save]; } -(IBAction)onTestButton2:(id)sender { NSMutableOrderedSet *exchange = [self.project mutableOrderedSetValueForKey:@"drawings"]; [exchange exchangeObjectAtIndex:0 withObjectAtIndex:1]; self.project.drawings = exchange; [self save]; Drawing *drawing = [self.project.drawings objectAtIndex:0]; BOOL current = [drawing.current boolValue]; drawing.current = [NSNumber numberWithBool:!current]; [self save]; } -(void)save { id delegate = [[UIApplication sharedApplication]delegate]; NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext]; NSError *error = nil; if( ![managedObjectContext save:&error] ) { NSLog(@"%@ Save: Unresolved Error on Save %@", error, [error userInfo] ); abort(); } } 

Now test by pressing the first test button. This sets the data.

Then press the second test button .... everything is OK !!!

Now press the second test button again and BANG. You should get an NSMergeConflict error like this ....

Save: Unresolved error while saving {conflictList = ("NSMergeConflict (0x8a7d0b0) for NSManagedObject (0x8bedfa0) with lens '0x8bd9340' with oldVersion = 1 and newVersion = 2, and old object snapshot = {\ n
current = \ "\"; \ n project = \ "0x8bc3f50 \", \ } and a new cached line = {\ n current = \ "\"; \ n project = \ "0x8aa88c0 \" \ } ");

I noticed from the error that the Project object has been modified. However, this is a single thread application using the main context of the application.

I spent too long on this and would like someone else to comment on where the problem is. Is this a kernel bug or am I the right "fool"?

Thank you very much

/ Fitto

+6
source share
1 answer

I had similar problems and a workaround was found here:

http://prod.lists.apple.com/archives/cocoa-dev/2013/Oct/msg00657.html

In short - add this to your MOC setup:

  [_managedObjectContext setMergePolicy:[[NSMergePolicy alloc] initWithMergeType:NSMergeByPropertyObjectTrumpMergePolicyType]]; 

This applies to OS X 10.9 Mavericks, as well as when using NSSQLStoreType. I could not repeat the Merge error using NSXMLStoreType.

For Swift 2.x

 managedObjectContext.mergePolicy = NSMergePolicy(mergeType: .MergeByPropertyObjectTrumpMergePolicyType) 
+9
source

Source: https://habr.com/ru/post/956989/


All Articles