AZCoreRecord icon indicating copy to clipboard operation
AZCoreRecord copied to clipboard

Add a method to save all parent contexts

Open tonyarnold opened this issue 13 years ago • 5 comments

Presently (and rightly) the save methods only save the context they are called on. However a common scenario is to want to save the changes up through any parents and persist the saved changes to disk. To that end, it would be useful to have a simple method that tracks back through any parents of the current context and saves them all in sequence, like so:

NSManagedObjectContext *parentMoc = [localContext parentContext];

do {
    [parentMoc save];
    parentMoc = [parentMoc parentContext];
} while (nil != parentMoc);

tonyarnold avatar Aug 27 '12 00:08 tonyarnold

I'd be all for this, and maybe it'd be a good addition to the save methods that have callbacks. My only concern is with the private parent contexts, like for iCloud and TICoreDataSync, which may not respond well to having save called. But otherwise I think it's great and I'll add it soon.

zwaldowski avatar Aug 27 '12 02:08 zwaldowski

Is there any way to detect that a parent context is being used for iCloud? (TICoreDataSync is easy enough to detect because of the NSManagedObjectContext subclass they use).

tonyarnold avatar Aug 27 '12 02:08 tonyarnold

A cursory glance at a Core Data class dump indicates that their private one is just a plain old NSManagedObjectContext, which complicates things as we don't want to set off any Apple private method checkers. The Ubiquity stuff just subscribes to a ton of notifications on that context.

zwaldowski avatar Aug 27 '12 02:08 zwaldowski

Would it be acceptable for this to be an extension to -saveWithErrorHandler:? So that it's something like -saveRecursive:success:failure:? This is inline with Apple's policy of matching up the most simple method (here, -save) with a big kahuna that has all the parameters.

zwaldowski avatar Aug 27 '12 15:08 zwaldowski

Yeah, absolutely. That makes a lot of sense! Good call :)

tonyarnold avatar Aug 27 '12 18:08 tonyarnold