English 中文(简体)
iPhone NSUserDefaults persistance difficulty
原标题:

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). After alot of searching and trying i got it to correctly save (and load) this data through the use of NSCoding and NSKeydArchiver.

Every screen in my app basically is a view of a little piece of information from this datamodel. Since one can change data in some of these views the data needs to be saved and loaded between screen transitions.

My screen view chain is this: root -> view 1 -> subview 1

Here comes the odd part:

In subview 1 i load the dataobject from the NSUserdefaults and present the information (this works), then the user can edit the data (in this case a NSString describing a company name) When the user presses the back button in the topbar to return to "view 1" it triggers a save (this also works).

Back at "View 1" a viewWillAppear event is triggered. In this trigger I reload the saved data from "subview 1" and redraw my screen (a UITableViewController). For some reason, after the load the changes made in the previous screen are not reflected! The loaded object still contains the same values as if the save hadn t happened!

After some logging it seems that even though i CALL the save before the LOAD, it apparently gets executed later. So the confirmed (by debug breakpoints) actions are -> LOAD -> SAVE -> LOAD. But according to the logfunction (i log each save and load call) it ends up as -> LOAD -> LOAD -> SAVE.

Can anyone help me with this? - I have no idea what i m doing wrong here.

This code is called from an object i create wherever i need it (in time it needs to be replaced with a singleton) and i access the saved and loaded data by MyDataStoreObject.currentData

- (void) saveCurrentData {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject: currentData];
    [defaults removeObjectForKey:@"MYAPP_CURRENTDATA"];
    [defaults setObject:data forKey:@"MYAPP_CURRENTDATA"];

    [defaults synchronize];
}

- (void) loadCurrentData {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSData *data = [defaults objectForKey:@"MYAPP_CURRENTDATA"];

    if (data == nil)
    {
        currentData = nil;
        NSLog(@"NO DATA TO RETRIEVE FROM USERDEFAULTS");
    }
    else
    {
        currentData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
}
问题回答

Call synchronize on your NSUserdefaults instance after you save, to ensure all the contents you save are written down and preserved immediately.

Edit:

In

When the user presses the back button in the topbar to return to "view 1" it triggers a save (this also works).

How did you do that? Show us the code that catches the back button being touched, I m curious if it performs the save every time.





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签