开发者

IPhone Application Design Decision?

开发者 https://www.devze.com 2022-12-21 10:33 出处:网络
I am writing a simple application that follows the MVC design pattern. In the Xcode project I have three bits:

I am writing a simple application that follows the MVC design pattern. In the Xcode project I have three bits:

  1. AppDelegate
  2. ViewController
  3. 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 cont开发者_JAVA技巧rols?

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


I see it as a matter of preference. At some point you'll need to invoke the model methods for saving/loading, which you've found a couple solutions for. In a project I'm building out now, I'm only going to load/save certain data when a user enters/exits a "subject" area. If it's too slow I'll likely do the loading of some data parts I need using a separate thread invoked at "applicationDidFinishLaunching". Then I'll check for thread completion when they click a button/tablecellselection.

Personally I'd consider building data load/save "wrapper" methods to hide most model variables (internals) and complexity, then have these wrappers simply return success/failure and maybe a ptr to the data.


Yes, applicationDidFinishLaunching: and applicationWillTerminate: are fine places to load/save your persistent data.

If you have lots of data to load and are concerned about slow application load time, then you might want to consider a more sophisticated strategy of loading things on demand (at which point you might want to look into using CoreData).

0

精彩评论

暂无评论...
验证码 换一张
取 消