IOS two lines of code to resolve data persistence

Source: Internet
Author: User
Tags value store

In the actual development of iOS, sometimes it involves saving the state of the program for the next recovery, or recording the user's preferences and the user's login information, etc. This involves the persistence of data, the so-called data persistence is the local preservation of data, the data from memory into the memory. There are many ways to persist data on the Internet, such as realizing I/O, database, cloud or third-party interface. But sometimes it is possible to simply do some simple data storage, such as user preferences, user SessionID, and so on, the use of the above method is a bit selectmen, now requires a more lightweight operation.

I. Understanding Nsuserdefaults

To find a solution to the above problem, check out Apple's official documentation found that there is a class nsuserdefaults designed specifically to address this problem:

  NSUserDefaults is a hierarchical persistent interprocess (optionally distributed)key-value store, optimized for storing user settings.

The translation is generally as follows:

NSUserDefaults 是一种进程间(任意分布)的分层级持久化键-值存储,为存储用户设置而优化。

Detailed instructions can be found in the official documentation, which only describes its use. Now that we've found a lightweight data persistence solution, why is it lightweight? Because Apple officially designed it to address user-set storage issues, here's how it's used.

Second, the use of nsuserdefaults

Because Nsuserdefaults is an inter-process solution, we can call it in any process to access and store the user's information. For example: We want to persist the data for the user's user name

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];[userDefaults setObject:@"whf" forKey:@"name"];

With the above two lines of code, we have stored the user's name in a key-value pair locally. No need to specify the storage location of the data, everything is done by the system, we just need to tell the system what we want to save. If the value of the same key is stored more than once, then the value of the key is determined by the last value, that is, the system overwrites the write, not the appended write, which returns the array.

The next step is to demonstrate the process of fetching data: In any thread, we call

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];NSString *userName = [userDefaults objectForKey:@"name"];

These two sentences can get the data we want from the memory, if the data does not exist, then the returned object is nil.

Third, the bottom realization mechanism

With the use of nsuserdefaults, it is found that the program re-run data is still present, then this data must be stored on the phone's memory. Now let's explore its implementation mechanism:

NSUserDefaults *userDefaults = www.90168.org[NSUserDefaults standardUserDefaults];    [userDefaults setObject:@"123" forKey:@"name"];    NSString *userName = [userDefaults objectForKey:@"name"];    NSLog(@"%@",userName);    NSString *homeDirectory = NSHomeDirectory();    NSLog(@"homeDire --------%@",homeDirectory);

Operation Result:


snip20160807_8.png

Follow the path into the sandbox discovery, library/preferences/in the sandbox A com.itripbuyer.date-persistence.plist plist file was found in the directory.


snip20160807_9.png

Opened and found there is a key value pair, and is the data we have just manipulated. So I guess, through our two lines of code, the system transforms our data into a plist file, which is loaded with some key-value pairs.

Four, flexible and skillfully used

Nsuserdefaults the use of the official given is to store the user's setting, but through the above operation found that the program is usually related to key-value pairs of storage, can use Nsuserdefaults to achieve, even if not the form of key-value pairs, The conversion to the key value pair also uses the nsuserdefaults to realize, this saves time and effort, but also can use the most concise code in exchange for the most stable data persistence operation.

IOS two lines of code to resolve data persistence

Related Article

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.