I am writing a simple application that follows the MVC design pattern. In the Xcode project I have three bits:
- AppDelegate
- ViewController
- DataModel
The DataModel object holds all the data for the application and I want to make sure it is saved on app-exit and reloaded at app-startup.
I am not sure if this is the correct approach, I have conformed my DataModel to NSCoding and added the encodeWithCoder: and initWithCoder: methods. I have declared my DataModel in my viewController as:
FlowerStore *flowerStore = [[FlowerStore alloc] init];
It is my understanding that I need to save and load using the AppDelegate methods listed below:
-(void)applicationDidFinishLaunching:(UIApplication *)application { // LOAD
-(void)applicationWillTerminate:(UIApplication *)application { // SAVE
Can anyone point me in the right direction for how I should be doing this? All the examples that I can find archive (or unarchive) the data directly within the object concerned (see below), where I need to do my loading and saving as the app starts/exits?
-(void)encodeWithCoder:(NSCoder *)encoder {
-(id)initWithCoder:(NSCoder *)decoder {
Any help / info is very much appreciated.
EDIT_001:
I just checked in the book "Beginning iPhone3 Development" where there is a section on achiving "Chapter 11 Data Persistence" and they do something that I would have thought would have been bad design. In the book they conform their dataModel to [NSCoding] and implement the methods to encode/decode. They then use viewDidLoad: and applicationWillTerminate: in their viewController to unachieve / achieve at the appropriate time. What I find bad is that they get the data from the UITextFields? I have data that I wish to store that is not represented on the UI so that won't work in my case. Also should you not be archiving / unachiving from the dataModel, not looking at the view via the UI controls?
EDIT_002:
Getting closer, it now looks like you do the following: (both in viewController)
-(void)viewDidLoad { // LOAD
-(void)applicationWillTerminate:(NSNotification *)notification { // SAVE
NOTE: that applicationWillTerminate uses NSNotification and not UIApplication, plus you also need to add the following code to the bottom of viewDidLoad.
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:app];
gary