How iOS data storage is summarized

Source: Internet
Author: User

There are four folders in the iOS sandbox directory. respectively:

-documents: All application Data files should be written to this directory. This directory is used to store user data or other information that should be backed up regularly.
-appname.app: This is the application's package directory, which contains the application itself. Because the application must be signed, you cannot modify the contents of this directory at run time, which may cause the application to fail to start.
-library: There are two subdirectories under this directory: Caches and Preferences
Preferences: Contains the application's preferences file. Instead of creating a preference file directly, you should use the Nsuserdefaults class to get and set your application's preferences.
Caches: Used to hold application-specific support files to save the information that is needed during application startup. -tmp: This directory is used to store temporary files, saving information that is not needed during the application restart. For persistent storage of data, 4 different mechanisms are generally available in iOS.
1.NSUserDefaults
2. Object archiving
3. Database Storage (SQLite)
4.Core data 1.NSUserDefaults in iOS development, nsuserdefaults is usually used to save small amounts of user settings or application state.
The data formats supported by Nsuserdefaults are: NSNumber (Integer, Float, Double), Nsstring,nsdate,nsarray,nsdictionary,bool type. 2. Object archiving uses two classes: Nskeyedarichiver, nskeyedunarchiver You can use the Nskeyedarchiver class to implement a data model cache. In order to archive model objects with Nskeyedarchiver, model classes need to follow the Nscoding protocol.

Nscoding protocol method-(void) Encodewithcoder: (Nscoder *) Acoder;
-(ID) Initwithcoder: (Nscoder *) Adecoder;
When the model follows the Nscoding protocol, the archive object is simple, as long as one of the following methods is called:
[Nskeyedarchiver archiverootobject:objectforarchiving Tofile:archivefilepath];
[Nskeyedarchiver archiveddatawithrootobject:objectforarchiving];
The first method creates an archive file under the path specified by Archivefilepath.
The second method returns a NSData object. NSData is usually faster because there is no file access overhead, but the object is saved in the app's memory, and memory is quickly exhausted if unchecked.
The Nskeyedunarchiver class is used to reverse-archive a model from a file (or NSData pointer). Depending on the location of the anti-archive, choose to use the following two class methods.[Nskeyedunarchiver Unarchiveobjectwithdata:data];
[Nskeyedunarchiver Unarchiveobjectwithfile:archivefilepath];
These 4 methods can be useful for converting serialized data.
The premise of using any nskeyedarchiver/nskeyedunarchiver is that the model implements the Nscoding protocol. 3. Database storage (SQLite), like Android, also uses the embedded database of SQLite in iOS. SQLite is a lightweight relational database. When using, only need to join Libsqlite3.dylib dependency and introduce sqlite3.h header file. FMDB is the library that encapsulates the SQLite API, and is a great source in the open community. The Fmdb is compatible with both arc and non-ARC projects and automatically adjusts the associated memory management code according to the engineering configuration. Use Fmdb to refer to the project documentation on GitHub: Https://github.com/ccgus/fmdb 4.Core data has not been used for the time being and study is pending.

How iOS data storage is summarized

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.