In the generated code for core data stack, a save on managedObjectContext is called in applicationWillTerminate method. This is to take care of all unsaved objects in the context before exit.
This has an unwanted side effect of saving any temporary objects as well. (This problem is visible in apple sample codes as well. If you click on + to add new Recipe in Recipe sample code, and then press Home button to terminate the app, a half done Recipe object is visible when you start the app next time).
What is the preferred design pattern for handling this issue? I could think of the following.
- Use a different scratchpad managedObjectContext for temporary objects and when you decide to really Save, then push a copy in the main managedObjectContext. Call save only on main managedObjectContext in applicationWillTerminate. (Is there any easy and fast way to move an object from one managedObjectContext to another apart from creating a copy manually in the new context?)
- Remove save from applicationWillTerminate, but ensure that all Objects are saved immediately after changes are made. (This may not easy all the time, I have a Tabbed application, a user may have initiated editing operations at the same time).
Let me know if there is a better way to handle this.