Nsuserdefaults Occasional/Sometimes save data will fail/expire

Source: Internet
Author: User

Previously implemented through Nsuserdefaults to save user data:

An array of saved custom objects (Object/custom Class) in the resolved Iphone/ios (Nsmutablearray/nsarray) to Nsuserdefaults

After the program exits, the next time it is reopened, the previous data is loaded.

But now continue to debug the process, found a problem, that is, sometimes nsuserdefaults save data is not successful, occasionally will fail, resulting in changes to the program data, no save, restart the app, change invalid, or change the effect before.

For example, the current list would be:

Changed to:

corresponding to the internal data, has been called Setobject:forkey: to save to the nsuserdefaults.

But after you close the app and restart it, you see that the change is not working, or the effect before you change it:

But more strangely, to debug a lot of times, the results occasionally found that this change can be effective, that is, the modified data, but also successfully saved to the nsuserdefaults.

"Resolution Process"

1. Debugging a lot of times, during, encountered many very silent things, Xcode debugging function, not the general garbage ah.

If you want to see the value of a current variable, the result is not directly viewable.

In short, Xcode debugging function, really is too garbage.

Detailed procedures, reference:

"Finishing" the debugging function in Xcode, it's so rubbish!!!

2. Finally after the toss, after debugging a half-day, suddenly thought, as if before Nsuserdefaults's official website introduction, mentioned a synchronize problem.

So I went back to see:

Nsuserdefaults Class Reference

And then found the hint:

At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user ' s defaults database. NSUserDefaults Caches the information to avoid have to open the user's defaults database each time you need a default value. synchronize the method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user ' s def Aults database.

So, I went to see Synchronize again:

Synchronize

Writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what's on dis K.

-(BOOL) Synchronize

Return Value

YESIf the data is saved successfully to disk, otherwise NO .

Discussion

Because This method was automatically invoked at periodic intervals, use this method only if you cannot wait for the Automa Tic Synchronization (for example, if your application are about to exit) or if you want to update the user defaults to what Is on disk even though you has not made any changes.

Availability
    • Available in IOS 2.0 and later.
See Also
    • – persistentDomainForName:
    • – persistentDomainNames
    • – removePersistentDomainForName:
    • – setPersistentDomain:forName:
Related Sample Code
    • Appprefs
    • Drilldownsave
    • International Mountains
    • Listadder
Declared in

NSUserDefaults.h

So, you can try it out in the code.

3. After adding the synchronize, the code becomes:

?
12345678 - (void)saveBirdSightingList{    //save data    NSData *encodedCurBirdSightingList = [NSKeyedArchiver archivedDataWithRootObject:self.masterBirdSightingList];    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    [defaults setObject:encodedCurBirdSightingList forKey:@"BirdSightingList"];        [defaults synchronize];}

Then the result solves the problem that nsuserdefaults occasionally does not work.

Each new change of data can be synchronized, so even if the program immediately exit, the changes can be saved in a timely manner. The next time you restart the program, you can also recover the latest data.

Summary

Nsuserdefaults occasionally does not work, unable to save the changed data because of the nsuserdefaults mechanism is that, over a period of time, will automatically call their own function synchronize to synchronize the data.

And if you are experiencing and I like here, after changing the data, then quit the program, that is, before the nsuserdefaults can synchronize before exiting the program, you need to manually call synchronize to save the data.

That is, when needed, manually call Nsuserdefaults to perform synchronous synchronize actions, in order to save (modified) data in a timely manner.

Nsuserdefaults Occasional/Sometimes save data will fail/expire

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.