zwaldowski / AZCoreRecord

Effortless fetching, saving, importing, and ubiquity for Core Data
MIT License
38 stars 1 forks source link

Add a method to save all parent contexts #9

Open tonyarnold opened 12 years ago

tonyarnold commented 12 years ago

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);
zwaldowski commented 12 years ago

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.

tonyarnold commented 12 years ago

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).

zwaldowski commented 12 years ago

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 commented 12 years ago

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.

tonyarnold commented 12 years ago

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