1

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}" )},

Fittoburst
  • 2,215
  • 2
  • 21
  • 33
  • Is the mutableCopy something you should be doing ? Isn't this create a copy of the drawings rather than simply changing the order? And are you keeping a reference to the old copy and making changes to that rather than the new copy? I have not used ordered relationships on Core Data but have had similar merge issues. Try a new fetch to get the drawings after the sort or reset the MOC perhaps. – Duncan Groenewald Oct 28 '13 at 02:02
  • Thanks but no....Can you try this for me...? http://stackoverflow.com/questions/19670429/ios7-nsmergeconflict-on-single-thread-save I'm keen to know if this is a bug in core-data – Fittoburst Oct 30 '13 at 07:09

0 Answers0