views:

44

answers:

2

When updating the managedObjectContext is it ok practice to do the save setup in view controllers that may be released or should the appDelegate handle the saving of the managedObjectContext so that even if the viewController is released the save finishes?

I'm leaning towards the idea of moving the save step into my appDelegate and having viewControllers call [appDelegate saveContext]; when an update is made, though perhaps thats moot since the viewController won't finish releasing until its done saving to CD either way...?

For instance, is there any difference between these two actions, done from a subViewController:

[appDelegate.managedObjectContext save:&error]

and

[appDlegate saveContext]

Where there is a method in appDelegate that runs [managedObjectContext save:&error]

Thanks, Sam

A: 

If you're just using one managedObjectContext through your app, it's not a bad idea to keep the save functionality in the app delegate, so that regardless of the state of your view controllers through the application's lifecycle, any updates will be saved when the app terminates.

That being said, I've found it useful to add additional save points, sometimes in view controllers, that will save the database after doing some significant updates. This way I've got my data saved even if the app crashes or the final save operation is otherwise prevented from completing.

I'm a relative newbie to Core Data so please inform me if this is bad practice.

pix0r
Yea, Thats the way I'm leaning. I wonder if having the [context save] step inside the viewController will cause problems if the VC is released or if it won't fully release until the [context save] finishes.
Shizam
A: 

Guessing that your application is single threaded, you are guaranteed that the save will finish before the view controller is released because the thread will block on the save.

If you are running a multi-threaded application; then

  1. You should not be saving on the background thread, that is dangerous in Core Data.

  2. You should not be accessing the view controller on the background thread because all view related activity should be performed on the main thread.

Therefore, there is no "correct" situation where you would have to worry about a -save: not finishing no matter what object you call it from because it is a blocking call.

Marcus S. Zarra
Ah yes, I forgot. I am running a multi-threaded application and I'm updating coreData according to the suggestions in the documentation for threaded apps (making a copy and update in the spun-thread and doing the merge on the main-thread). Thanks for the confirmation in a single threaded environment though.
Shizam
As I mentioned, even if you are in a multi-threaded app, your save is not going to cross thread boundaries and therefore it is going to block the thread that it is on and therefore you are still safe to call `-save:` from any object as long as you release that object after calling `-save:`
Marcus S. Zarra