Have got an issue with my NSOrderedSets.
I have coredata entities - 'Project' and 'Drawing'. A Project has many Drawings. The relationship is One-to-Many and Ordered, hence my Project object contains an NSOrderedSet of Drawings.
My app is single-threaded and is causing me headaches if wish I re-order my Drawing objects.
I re-order the Drawings with the following code...
-(IBAction)onTestReOrder:(id)sender
{
NSMutableOrderedSet *exchange = [self.currentProject.drawings mutableCopy];
[exchange exchangeObjectAtIndex:0 withObjectAtIndex:1];
self.currentProject.drawings = exchange;
// Save
id delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext];
NSError *error = nil;
if( ![managedObjectContext save:&error] )
{
NSLog(@"%@ Save: Unresolved Error on Save %@, %@", methodName, error, [error userInfo] );
abort();
}
}
This all seems to work well. My underlying Sqlite datastore appears to be updated to reflect the re-order.
My problem arises when I try to write a change to a property within my Drawing after the re-order. For instance...
drawing = [self.currentProject.drawings objectAtIndex:1];
drawing.current = [NSNumber numberWithBool:YES];
// Save....causes NSMergeConflict
id delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext];
NSError *error = nil;
// BANG ON SAVE....NSMergeConflict
if( ![managedObjectContext save:&error] )
{
NSLog(@"%@ Save: Unresolved Error on Save %@, %@", methodName, error, [error userInfo] );
abort();
}
The Save call here creates an NSMergeConflict. Looking at the snapshots, it seems the Drawing's Project is different between Old and New but this is a single-threaded app and only one ManagedObjectContext. How can I have different references to the 'Project'?
I'm pulling my hair on this one and any hints to help me resolve the NSMergeConflict are greatly appreciated.
/Fitto
Error...
2013-10-26 23:40:38.047 testDesign[34625:a0b] setCurrentDrawing Save: Unresolved Error on Save Error Domain=NSCocoaErrorDomain Code=133020 "The operation couldn’t be completed. (Cocoa error 133020.)" UserInfo=0xc148180 {conflictList=( "NSMergeConflict (0xc151860) for NSManagedObject (0xb3f0030) with objectID '0xb379040 ' with oldVersion = 58 and newVersion = 59 and old object snapshot = {\n angle = 0;\n current = 1;\n depth = \"4.8768\";\n project = \"0xb3eec60 \";\n offsetX = 0;\n offsetY = 0;\n type = 0;\n width = \"9.7536\";\n} and new cached row = {\n angle = 0;\n current = 1;\n depth = \"4.8768\";\n project = \"0xc1f3cb0 \";\n offsetX = 0;\n offsetY = 0;\n type = 0;\n width = \"9.7536\";\n}" )},