List of properties for iOS data storage understanding

Source: Internet
Author: User

List of properties for iOS data storage understandingIntroduction to data storage

Data storage, or data persistence, refers to the way in which the application's data is saved.
My understanding is that once an application has been developed, the application will generate a lot of data when it runs in memory, which resides in memory with the program as it runs, and disappears as soon as the program runs out of memory. Wait until you run the program again, and the previous data is recalculated again. However, for some applications, we need to persist the data generated by the program so that the data will not be lost after the application restarts, and the data persistence technology is needed.
There are a number of mechanisms for persisting data persistence on iOS devices, with the following 4 more common:

    1. Property List

    2. Object archiving

    3. Embedded relational database for iOS (SQLite)

    4. Apple provides a tool for the persistence of core Data
      Besides, there are nsuserdefaults, preference, etc.

Property List

Start by learning from the list of properties
The attribute list provides a convenient way to store simple structured data. Only serialized objects can be stored in the property list. A serialized object is an object that can be converted to a byte stream for easy storage into a file or transmitted over a network. Although any object can be serialized, only certain objects can be placed into a collection class (such as Nsdictionary or Nsarray) in a property list mechanism that stores the object's related data in a file that is suffixed with. plist. When the program reloads, the data is read from the. plist file.

Sand Box

Briefly describe the sandbox mechanism for iOS apps before you introduce a list of properties. iOS apps can only read files in the file system created for the program, not anywhere else, and this area becomes a sandbox, so all non-code files are saved here.

View the App catalog

For a more intuitive understanding of the property list mechanism, we create a project on the Mac and generate the appropriate app to run the app in the virtual machine. Because of the sandbox mechanism, we cannot directly view the file directories that are applied to the virtual machine. You can install Simpholers on your Mac for easy viewing of the file directories in your virtual machine, and after installation, you can click the button above the project (in red box) to see the directories that are applied to the virtual machine.

Then click on the corresponding app name in the pop-up drop-down box

Next, you can test the directory structure of your application, such as

The. plist file generated by the property list will be placed in the application's documents directory

Case

The following properties apply:

@property (strongnonatomic) IBOutletCollection(UITextFieldNSArray *lineFields;

That is, we set up a text box group for this app as its properties,
The application interface is as follows :

Our goal is to run the app, fill in the data in the text box group, and write the contents of the text box to the. plist file once the app has retreated to the background or exited directly from the system. The next time you start the app, automatically reads the contents of the. plist file, and then copies the contents of the corresponding text box in the record to the text box in the app

Get. plist the path you want to save, the code is as follows:

- (NSString*) datafilepath{Nsarray*paths =Nssearchpathfordirectoriesindomains(nsdocumentdirectory,Nsuserdomainmask,YES);NSString*documentsdirectory = [Paths Objectatindex:0];return[Documentsdirectory stringbyappendingpathcomponent:@ "Data.plist"]; }

This code mainly does the following things:
(1) Find documents directory;
(2) After finding, add a data file in the directory data.plist;
(3) Full path to save data file Data.plist
Read the data from the. plist file with the following code:

- (void) viewdidload{[SuperViewdidload];additional setup after loading the view, typically from a nib.    NSString*filepath = [ SelfDataFilePath];NSLog(@"%@", FilePath);if([[NsfilemanagerDefaultmanager] Fileexistsatpath:filepath]) {Nsarray*array = [[NsarrayAlloc] Initwithcontentsoffile:filepath]; for(inti =0; I <4; i++) {Uitextfield*thefield = Self. Linefields[i]; Thefield. Text= Array[i]; }    }uiapplication*app = [uiapplicationSharedapplication]; [[NsnotificationcenterDefaultcenter] Addobserver: SelfSelector@selector(applicationwillresignactive:) Name:uiapplicationwillresignactivenotificationObject:app];}


(1) Check that the data file exists, and if it does not, do not load it. If present, instantiate the array with the contents of the file

(3) after loading the data from the attribute list, a reference to the application instance is given, which subscribes to the uiapplicationwillresignactivenotification, that is, the current object sets itself as the observer and observes its own behavior. If it exits from the foreground, it receives a uiapplicationwillresignactivenotification notification and then executes the contents of the Applicationwillresignactive method. The last parameter app refers to the app that publishes the notification, and you can see that the notification issuer is itself
Write the data to the. plist file with the following code:

-(void ) applicationwillresignactive: (nsnotification  *) notification{nsstring  * FilePath = [self  DataFilePath]; nsarray  *array = [self  .linefields  valueforkey:@ "text" ]; [Array Writetofile:filepath atomically:yes ];} 

This method looks simple, but actually does a lot of things. First, a string array is constructed by the text method of each text box in the Linefields array, and then the contents of the data are written to a property list file. Use the attribute list to save the data.
The entire list of properties is covered, then run the program to see the results.

Results

first entry, the text box in the application is blank, the Documents folder does not have a file




you see, The Documents folder has generated a data.plist file
we can open the file and view its file contents, such as:

In fact, the. plist file format is XML, and a preview of the Data.plist file confirms this fact.

Summary

Through the understanding of the attribute list theory and the actual operation, the mechanism of the attribute list is understood. In fact, I think the property list is to write the properties of an object to a file, which is essentially storing the data in a file. When the app exits from the foreground, the data is written to the property list file, and the data is read in the Properties list file when the app is rerun. It is important to note that not all properties can be written to the property list. If the object contains a custom object as its property, this custom object property will not be serialized. There are several OC classes that can be serialized:
Nsarray, Nsmutablearry, Nsdictionary, Nsmutabledictionay, NSData, Nsmutabledata, NSString, NSMutableString, NSNumber, NSDate.
From the add operation of the Data.plist file, you can see the following types of attributes added:

We can probably understand that the data stored in the attribute list is a built-in attribute in OC, which is suitable for storing files with little data content such as configuration files.

List of properties for iOS data storage understanding

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.