IOS optimized Memory (eight) persistence

Source: Internet
Author: User

iOS persistence
1. File system

File systems, whether Mac OS X or iOS, are built on UNIX file systems.

1.1 Sandbox model

In iOS, an app's read and write permissions are limited to its own sandbox directory.

What are the benefits of sandbox models?
Security: Other apps can't modify your program or data
Protect privacy: Other apps can't read your programs and data
Easy to delete: Because all of the content generated by an app is in its own sandbox, deleting the app simply removes the sandbox and removes the program completely.

Directories in the IOS app sandbox

    • App bundles, such as Xxx.app, are actually a directory with binary data and resource files for the app itself
    • Documents, storing the document data generated by the program
    • Library, the following default contains the following two directories Caches Preferences
    • TMP, temp file directory

What should we do if we want to get the path to one of the above directories in the program? Let's talk about the path acquisition, through the Nssearchpathfordirectoriesindomains function in NSPathUtilities.h, we can get the path we want. This function is specifically declared as follows:

Nsarray *nssearchpathfordirectoriesindomains (nssearchpathdirectory directory, Nssearchpathdomainmask DomainMask, BOOL Expandtilde);
directory types such as the documents directory are nsdocumentdirectory
Domainmask in the iOS program this takes Nsuserdomainmask
Expandtilde YES, means to expand to full path

Note that the type returned by the function is an array, which in iOS generally contains only one element, so take lastobject directly.

1.2 Nsfilemanager

Nsfilemanager provides a class method to obtain a single case.

  
 
    1. < span class= "comment" style= "margin:0px; padding:0px; Border:none; Color:rgb (0,130,0); Background-color:inherit ">/* returns the default singleton instance.*/  +  (nsfilemanager *) defaultmanager; 

Here is a list of common methods of Nsfilemanager

    • New Catalog

  
 
  1. -  ( bool ) Createdirectoryatpath: (nsstring *) Path withintermediatedirectories: ( bool ) Createintermediates attributes: (nsdictionary *) Attributes error: (NSError **) error; 

Createintermediates This parameter is generally yes, which means that if a directory in the middle of the directory path does not exist then create it, if no, then make sure the parent directory of the created directory must already exist

    • Get all files under the directory
 
   
  
  1. -(Nsarray *) Contentsofdirectoryatpath: (NSString *) path error: (NSERROR * *) error;

If the directory is empty, an empty array is returned

    • A few other ways

  
 
  1. - (BOOL) Copyitematpath: (NSString *) Srcpath Topath: (NSString *) Dstpath Error: (NSERROR *) error; - (BOOL) Moveitematpath: (NSString *) Srcpath Topath: (NSString *) Dstpath Error: (NSERROR *) error; - (BOOL) Linkitematpath: (NSString *) Srcpath Topath: (NSString *) Dstpath Error: (NSERROR *) error; - (BOOL) removeItemAtPath: (NSString *) path error: (NSERROR * *) error;

More can view the document Nsfilemanager Class Reference.

In a real project, we typically write a tool class that is responsible for all the path operations in the project.

2. Archiving (Archives) and serialization (Serializations)

We often hear the word "serialization", "deserialization", in fact, the meaning of "serialization" is to convert an object into a byte stream for preservation or transmission, "deserialization" is an opposite process, from byte to object.

In this section there is a file type plist,plist is the abbreviation of the property list, the so-called attribute lists, the attribute list has two data formats, one is XML, easy to read and edit, the other is binary, saves storage space, and improves efficiency.

In Objective-c, this object and the byte stream are divided into two categories:

    • Archive conversions between normal custom objects and byte streams
    • A conversion that serializes data between certain types (Nsdictionary, Nsarray, NSString, NSDate, nsnumber,nsdata) and byte streams (typically saved as plist files)

Essentially, however, both of these are conversions between the object graph and the byte stream. Apple programming guidelines for serialization and archiving: Archives and serializations Programming Guide.

2.1 Archive

What should we do if we need to save a custom object to a file?
Here are two things to be introduced: one is the nscoding protocol, and the other is that Nskeyedarchiver,nskeyedarchiver actually inherits from Nscoder, which can serialize and deserialize the object's properties in the form of a key-value pair.
The specific process can be described in such a way that by nskeyedarchiver the objects and byte streams that implement the Nscoding protocol can be converted to each other .

Like some of the data types in the framework such as nsdictionary,nsarray,nsstring ... have already implemented the Nscoding protocol, so they can be archived directly.

Here's a more complete example, an address class, a user class, and an address type attribute under the user class.

Address class

User class

Using the example

By viewing the contents of the file, you can find that the plist binary data format is saved. You can see the following as you go to XML:

2.2 Serialization

In an actual project, we typically save nsdictionary or Nsarray objects to a file or read from a file into an object. Of course, this is only applicable to the data volume is not a very large application scenario. Both Nsdictionary and Nsarray have a way to write files

Nsdictionary and Nsarray will be written directly into the plist file.

How 2.2.1 is serialized

Serialization can be done in two ways

Using the data object's own method

Write a file

The contents of the written document are as follows:


Now persist data with a third-party comparison!

IOS optimized Memory (eight) persistence

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.