The application completes the notification and saves drafts (master data)

If the user is currently in my edit view controller, I want to be able to save the changes when the user closes my application. For this, I am observing the UIApplicationWillTerminateNotification common application. In my application, I use the applicationWillTerminate: method to close it and free all of my main data contexts.

The problem I discovered is that the applicationWillTerminate: method in the applicationWillTerminate: delegate is called before any observers respond to the UIApplicationWillTerminateNotification notification. This means that my main data stack was released and closed before my editing editor saw a chance to save anything!

How this is usually overcome, because I do not see the way!

Many thanks,

Michael

+7
iphone core-data
source share
2 answers

Edit: The first (notification-based) approach probably won't work due to the inner workings of the startup loops and notifications.

If you want to adhere to the notification architecture, you can publish your own notifications in applicationWillTerminate: Just create your own MyApplicationWillTerminate notification, then call postNotification: on [NSNotificationCenter defaultCenter] . Then register your controller controller for MyApplicationWillTerminate instances, not the default UIApplicationWillTerminateNotification .

Another choice is for your application delegate to keep an instance of your edit controller, if one is visible, and then in applicationWillTerminate: save the new information before releasing the Core Data context. This contaminates the delegate of your application with additional instance variables, so this may not be the optimal solution.

One final thought: why not save the application, no matter what changes are made when the user makes them? This way, you don’t have to worry about the application being closed halfway through editing some of the information - the changes are already saved, and you can simply release the Core Data material as you already are. (Perhaps this is not suitable for you, I really could not say without knowing more about the structure of your application and the data you are editing.)

+4
source share

Not sure here, but does NSManagedObjectContext save the storage coordinator and object model? In this case, is it not enough for your controller to maintain the context of the managed entity?

0
source share

All Articles