How iOS persists data storage

Source: Internet
Author: User

1.plist file Storage

Each iOS app has its own app sandbox (the app sandbox is the file system directory) and is isolated from other file systems. Apps must stay in their sandbox, other apps can't access the sandbox

Apply the sandbox's file system directory as shown (assuming that the app's name is called layer)

The root path of the emulator app sandbox is: (Apple is the user name, 6.0 is the emulator version)

/users/apple/library/application Support/iphone simulator/6.0/applications

Document saves data that needs to be persisted when the app runs, and it backs up the directory when itunes synchronizes the device. For example, a game app can save a game archive in that directory

Temp saves the temporary data required to run the app, and then deletes the corresponding file from the directory when it is finished. When the app is not running, the system may also purge files in that directory. itunes does not back up this directory when syncing the device

Library/caches saves data that needs to be persisted when the app runs, and itunes syncs the device without backing up the directory. Non-critical data with large storage volumes and no backup required

Library/preference: Save all your app's preferences, and the iOS settings (settings) app will find the app's settings in that directory. This directory is backed up when itunes syncs the device

Here is the sample code:

1 /** Save Data*/2-(ibaction) savebtn3 {4NSLog (@"Save");5     //1. Get the sandbox root path6NSString *homepath =nshomedirectory ();7     //2.document Path8NSString *path = [HomePath stringbyappendingpathcomponent:@"Documents"];9     //3. New DataTenNsarray *array = [Nsarray arraywithobjects:@"nan",@( A), nil]; OneNsdictionary *dict = @{@"SSS":@"sddd",@"Ssssaw":@(1222)}; A      -      -     //4. Storing Data theNSString *fullpath1 = [path stringbyappendingpathcomponent:@"data1.plist"]; -NSString *fullpath2 = [path stringbyappendingpathcomponent:@"data2.plist"]; -      -[Array writetofile:fullpath1 atomically:yes];//Array Write plist + [Dict writetofile:fullpath2 atomically:yes]; -      +NSLog (@"dict-%@", dict); A } at  - /** Read Data*/ --(ibaction) readbtn - { -     //1. Get home directory -NSString *home =nshomedirectory (); in     //2. Stitching the document directory -NSString *path = [Home stringbyappendingpathcomponent:@"Documents"]; to     //3. File path +NSString *filepath1 = [path stringbyappendingpathcomponent:@"data1.plist"]; -NSString *filepath2 = [path stringbyappendingpathcomponent:@"data2.plist"]; the     //Read File *Nsarray *array =[Nsarray arraywithcontentsoffile:filepath1]; $Nsdictionary *dict =[Nsdictionary dictionarywithcontentsoffile:filepath2];Panax NotoginsengNSLog (@"array-%@, Dict-%@", array, dict); -}

Disadvantage: Only objects containing writetofile: methods, such as Nsdictionary,nsarray, can be stored.

2. Preferences--Storage directory Library/preference:

Many iOS apps support preferences such as saving usernames, passwords, font size, and so on, and iOS offers a standard set of solutions to add preferences to your app. Each app has a Nsuserdefaults instance that accesses preferences such as saving user names, font sizes, Whether to log on automatically

Here is the sample code

/** Save Data*/-(ibaction) save{//1. Using Nsuserdefaults, direct access to the software's preferences (library/preferences) is a singleton objectNsuserdefaults *defaults =[Nsuserdefaults Standarduserdefaults]; //2. Storing Data[Defaults setobject:@"SD"Forkey:@"User"]; [Defaults setobject:@"123w"Forkey:@"Test"]; [Defaults Setinteger: -Forkey:@" Age"]; [Defaults setbool:yes forkey:@"Auto_login"]; //3. Sync now (equivalent to update data)[Defaults synchronize]; }/** Read Data*/-(ibaction) read{//1. Setting Up Nsuserdefaults objectsNsuserdefaults *defaults =[Nsuserdefaults Standarduserdefaults]; //2. Reading dataNSString *user = [Defaults objectforkey:@"User"]; NSString*test = [Defaults objectforkey:@"Test"]; Nsinteger Age= [Defaults Integerforkey:@" Age"]; BOOL Autologin= [Defaults Boolforkey:@"Auto_login"]; NSLog (@"User-%@\n test-%@\n Age-%d\n autologin-%d\n,", User,test,age,autologin);}

Note: When setting the data, Userdefaults does not write immediately, but instead writes the cached data to the local disk according to the timestamp. So after calling the set method, it is possible that the data has not yet been written to the disk application to terminate. The above problem can be forced to write [defaults synchornize] by calling the Synchornize method;

Cons: Essentially or plist file storage, storing data faster than plist file storage.

How iOS persists data storage

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.