IOS Perfect for data persistence with two lines of code _ios

Source: Internet
Author: User
Tags value store

Objective

In actual iOS development, there are times when it comes to saving the state of a program for the next recovery, or to record some user preferences and user login information, and so on. This requires the persistence of data, the so-called data persistence is the local preservation of data, data from memory to move to memory. There are many ways to persist data on the web, such as implementing your own I/O, database, cloud or third party interfaces, and so on. But sometimes it may just do some simple data storage, such as user preferences, user SessionID, and so on, the use of this method is a bit selectmen, now need a more lightweight mode of operation.

I. Understanding of Nsuserdefaults

To find a solution to the problem, the official Apple document found that a class Nsuserdefaults was designed specifically to address this problem:

 Nsuserdefaults is a hierarchical persistent interprocess
 (optionally distributed) Key-value store, optimized for Stor ing user settings.

The translation is roughly as follows:

Nsuserdefaults is a tiered-level persisted key-value store between processes (arbitrarily distributed), optimized for storing user settings.

Detailed instructions can be found in the official documentation, which is only described in its use.

Now that we've found a lightweight data persistence solution, why do we say it's lightweight? Because Apple's official design is designed to address the storage problems that users have set up, here's how it's used.

Ii. Use of Nsuserdefaults

Since Nsuserdefaults is a inter-process solution, we can call it in any process to access and store the user's information.
For example: We're going to persist data persistence on the user's username.

Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
[Userdefaults setobject:@ "WHF" forkey:@ "name"];

With these two lines of code, we have stored the user's name in a key-value pair locally. Do not 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, the value of the key is determined according to the last value, that is, the system is overwrite write, not append write the last return is the array.

Next, we'll show you the process of fetching data: In any thread, we call

Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
NSString *username = [userdefaults objectforkey:@ "name"];

These two sentences can be obtained from the memory of the data we want, if the data does not exist, then the return of the object is nil.

Third, the underlying implementation mechanism

With the use of Nsuserdefaults, the Discovery program's rerun data still exists, and the data must be stored on the phone's memory. Now let's explore its implementation mechanism:

Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
 [Userdefaults setobject:@ "123" forkey:@ "name"];
 NSString *username = [userdefaults objectforkey:@ "name"];

 NSLog (@ "%@", userName);
 NSString *homedirectory = Nshomedirectory ();
 NSLog (@ "Homedire--------%@", homedirectory);

Run Result:

According to the path into the sandbox found, in the sandbox of the library/preferences/directory found in a more than one com.itripbuyer.Date-Persistence.plist of the plist files.

When I opened it, I found that there was a key value pair, and that's the data we just manipulated. So I guess, through the two lines of code we just had, the system converted our data into a plist file loaded with some key-value pairs.

Iv. flexible and skillful use

Nsuserdefaults The official purpose is to store the user's setting, but through the above operation found that the program in general involves key-value pairs of storage, can be implemented using nsuserdefaults, even if not the form of key-value pairs, The conversion of key-value pairs also use nsuserdefaults to achieve, so both time-saving and labor-saving, but also with the most concise code in exchange for the most stable data persistence operation.

That's what iOS does with two lines of code to get the most out of data persistence, and I hope this article helps when developing iOS.

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.