Common ways to store iOS app data:
- XML attribute list (plist) archive
- Preference (preference setting)
- Nskeyedarchiver Archive (nscoding)
- sqlite3--, I'm not talking here.
- Core data--here for the moment.
First, the application of sand box
- 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
- The file system directory where the sandbox is applied, as shown in
Second, the application of the sand box structure analysis
- Application Package: (layer in) contains all the resource files and executable files
- Documents: 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
- TMP: 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
III. implementation of XML attribute list (plist) archiving
- The attribute list is an XML-formatted file that expands to the name plist
- If the object is NSString, Nsdictionary, Nsarray, NSData, NSNumber, and so on, you can use the writetofile:atomically: method to write the object directly to the property list file
- Code implementation:
1 //Save Data2-(Ibaction) Saveclick: (ID) Sender {3 //plist Storage Note: Custom objects cannot be stored. Suitable for accessing arrays and dictionaries4Nsarray *arr = @[@"121",@4];5 //get the App sandbox (you can understand the folder that corresponds to the application)6 //nsstring *home = Nshomedirectory ();7 //NSLog (@ "%@", home);8 9 //gets the cache file path,Ten //nssearchpathfordirectoriesindomains (nssearchpathdirectory directory, Nssearchpathdomainmask DomainMask, BOOL Expandtilde); One //nssearchpathdirectory: Search for Directories A //Nssearchpathdomainmask: Scope of search Nsuserdomainmask: Indicates a lookup on a user's phone - //Expandtilde: Whether to expand the full path, if not expanded, the application of the sandbox path is ~ - //Store Be sure to expand the path theNSString *cachepath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0];//The return value is an array, and a project cache file is a - - //file name of stitching saved -NSString *filepath = [CachePath stringbyappendingpathcomponent:@"arr.plist"]; + //Save - [arr Writetofile:filepath atomically:yes]; + ANSLog (@"saved successfully"); at}
1 //reading Data2-(Ibaction) Readclick: (ID) Sender {3 //get the cache file path4NSString *cachepath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0];5 6 //file name of stitching saved7NSString *filepath = [CachePath stringbyappendingpathcomponent:@"arr.plist"];8Nsarray *arr =[Nsarray Arraywithcontentsoffile:filepath];9NSLog (@"%@", arr);Ten}
Iv. Preference (Preferences)--preferences storage, used objects--nsuserdefaults (Singleton object)
- Benefits: 1. No need to care about file name 2. Fast Key-value-to-store
- Code implementation:
1 //Click Save2-(Ibaction) Save: (ID) Sender {3 //Nsuserdefaults is a singleton object4Nsuserdefaults *userdefaults =[Nsuserdefaults standarduserdefaults];5 //Storage Account--account:guochao6[Userdefaults SetObject:@"Guochao"Forkey:@" Account"];7 //Store password--pwd:1238[Userdefaults SetObject:@"123"Forkey:@"pwd"];9 //Storage Age--age:30Ten[Userdefaults Setinteger: -Forkey:@" Age"]; One A //before iOS7, the default does not immediately synchronize the hard disk, iOS7 after the need for - //Synchronizing Code - [Userdefaults synchronize]; the}
1 //Click to read2-(Ibaction) Read: (ID) Sender {3NSString *account = [[Nsuserdefaults standarduserdefaults] Objectforkey:@" Account"];4NSString *pwd = [[Nsuserdefaults standarduserdefaults] Objectforkey:@"pwd"];5Nsinteger age = [[Nsuserdefaults standarduserdefaults] Integerforkey:@" Age"];6NSLog (@"%@--%@--%ld", account,pwd,age);7}
V. Nskeyedarchiver Archive (nscoding)--Custom object archiving
- If the object is NSString, Nsdictionary, Nsarray, NSData, NSNumber and other types, you can archive and restore directly with Nskeyedarchiver
- Not all objects can be archived directly in this way, only objects that comply with the Nscoding protocol can
- There are 2 ways to nscoding a protocol:
- Uencodewithcoder:
- This method is called every time the object is archived. In this method, you typically specify how to archive each instance variable in an object, and you can use the Encodeobject:forkey: method to archive instance variables
- Uinitwithcoder:
- This method is called every time the object is recovered (decoded) from the file. In this method, you typically specify how to decode the data in a file as an instance variable of an object, and you can use the Decodeobject:forkey method to decode the instance variable
Precautions! Note For--nskeyedarchiver-archive objects:
- Should add a sentence to the Encodewithcoder: method
- [Super Encodewithcode:encode];
- Ensure that inherited instance variables can also be encoded, which can also be archived
- Should add a sentence to the Initwithcoder: method
- self = [super Initwithcoder:decoder];
- Ensure that inherited instance variables can also be decoded, which can also be restored
Code implementation:
1 //Custom Object Archiving2-(Ibaction) Save: (ID) Sender {3Person *p =[[Person alloc] init];4P.name =@"Guochao";5P.age =23232;6 //get the cache file path7NSString *cachepath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0];8 9 //Stitching file PathTenNSString *filepath = [CachePath stringbyappendingpathcomponent:@"Person.data"]; One //Archive A [Nskeyedarchiver archiverootobject:p Tofile:filepath]; - } - the-(Ibaction) Read: (ID) Sender { - - //get the cache file path -NSString *cachepath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0]; + - //Stitching file Path +NSString *filepath = [CachePath stringbyappendingpathcomponent:@"Person.data"]; A //solution file atPerson *p =[Nskeyedunarchiver Unarchiveobjectwithfile:filepath]; -NSLog (@"%@", p.name); -}
iOS Edge Learning – Three of the common ways to apply data storage (Plist,preference,nskeyedarchiver)