3

I'm new with Core Data and I have some questions about how to do the things in the right way. I think I need a temporaryManagedObjectContext to work with temporary entities, while user is editing a new entity. When user tap save that data I want to insert those entities in the persistedManagecObjectContext and then saveContext. What is the best practice to achieve this? Since I don't want to save in the temp context, it is mandatory to use threading?

Thanks for your knowledge!

Samui
  • 1,384
  • 3
  • 14
  • 28
  • I haven't got time to write a full answer, but you could try using multi-context Core Date as described here http://www.cocoanetics.com/2012/07/multi-context-coredata/ – Abizern Jun 10 '13 at 14:47

2 Answers2

6

You need to merge the changes in the temporary ManagedObjectContext (MOC) into the persisted one. This is an example of how I achieved this. Im using threading (One MOC per thread), but Im pretty sure it should work out fine without threads aswell.

You call this method to save your changes in the tempMOC:

- (void) saveNewEntry {
    NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];

    // Subscribe to "NSManagedObjectContextDidSaveNotification"
    // ..which is sent when "[tempMOC save];" is called.
    [dnc addObserver: self 
            selector: @selector(mergeChanges:) 
                name: NSManagedObjectContextDidSaveNotification 
              object: tempMOC];

    // Save changes
    [tempMOC save];

    // Remove subscribtion
    [dnc removeObserver: self 
                   name: NSManagedObjectContextDidSaveNotification 
                 object: tempMOC];

}

...which will fire off a notification to:

- (void) mergeChanges: (NSNotification*) saveNotification {

    // If youre using threads, this is necessary:
    [self performSelectorOnMainThread: @selector(mergeToMainContext:) 
                           withObject: saveNotification 
                        waitUntilDone: NO]; 

    // ...otherwise, you could do the merge in this method
    // [persistedMOC mergeChangesFromContextDidSaveNotification: saveNotification];
}

...which in turn calls:

- (void) mergeToMainContext: (NSNotification*) saveNotification {

    [persistedMOC mergeChangesFromContextDidSaveNotification: saveNotification];

}
Mani
  • 1,597
  • 15
  • 19
0

I have the same issue (editor needs an object, so create... but if cancel, throw away that object). I ended up with a more brute-force approach, though, precisely to avoid all the multiple objet contexts and merging:

I use a "is temp" field in all my objects. When I need to create a "provisional" object I have a method in my data layer that creates an object as normal, but then flips isTemp=true before returning.

Then, in my "object editor" onCancel there is:

if (obj.isTemp) {
    [context deleteObject: ... ];
}

onSave is:

if (obj.isTemp)  obj.isTemp = NO;
[context saveAsUsual...];

Note: not addressed here there's the issue of not copying "throw away" changes into an existing object until user confirms saving. Otherwise changes would be sitting there like a trojan horse waiting to be applied when some other code saves the shared context.

Bill Patterson
  • 2,495
  • 1
  • 19
  • 20