Common ways for iOS app data storage
- XML attribute list (plist) archive
- Preference (preference setting)
- Nskeyedarchiver Archive
- SQLite3
- Core Data
Apply Sandbox
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 structure of the sandbox:
- Application Package: 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
Common ways to get the sandboxed catalog applied
L Sandbox root directory: nsstring *home = Nshomedirectory ();
Documents: (2 ways)
(1) Use the sandbox root to stitch the "Documents" string NSString*home =nshomedirectory (); NSString*documents = [Home stringbyappendingpathcomponent:@"Documents"];//not recommended because a new version of the operating system may modify the directory name(2) using the Nssearchpathfordirectoriesindomains function//Nsuserdomainmask representative from the user folder to find//YES represents the wavy character "~" in the expanded pathNsarray *array =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, NO);//in iOS, only one directory matches the parameters passed in, so there is only one element in the collectionNSString *documents = [Array Objectatindex:0]
Tmp:
NSString *tmp = nstemporarydirectory (); Library/Caches: (2 methods similar to documents) use the sandbox root to stitch the "Caches" string with the Nssearchpathfordirectoriesindomains function ( Change the function's 2nd parameter to: nscachesdirectory) Library/preference: Access the settings information in this directory through the Nsuserdefaults class
Property List The attribute list is an XML-formatted file that expands to be named Plist if the object is NSString, Nsdictionary, Nsarray, NSData, NSNumber, and so on, you can use writetofile:atomically: method to write the object directly to the property list file Attribute List-Archive nsdictionary Archive a Nsdictionary object to a list of plist properties
//encapsulate data into a dictionaryNsmutabledictionary *dict =[Nsmutabledictionary dictionary]; [Dict setobject:@ "Hello"Forkey:@"name"]; [Dict setobject:@"15013141314"Forkey:@"Phone"]; [Dict setobject:@" -"Forkey:@" Age"];//persisting a dictionary to a documents/stu.plist file[Dict Writetofile:path Atomically:yes];
Property List-Restore Nsdictionary
//reads the contents of the documents/stu.plist, instantiates the nsdictionaryNsdictionary *dict =[Nsdictionary Dictionarywithcontentsoffile:path]; NSLog (@"name:%@", [Dict Objectforkey:@"name"]); NSLog (@"phone:%@", [Dict Objectforkey:@"Phone"]); NSLog (@"age:%@", [Dict Objectforkey:@" Age"]);
Preference settings
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 to access the preferences. For example, save the user name, the font size, whether to log on automatically
Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];[ Defaults setobject:@ "itcast" forkey:@ "username"]; [Defaults setfloat: 18.0f forkey:@ "text_size"]; [Defaults setbool:yes forkey: @" Auto_login "];
Read the Last saved settings
Nsuserdefaults *defaults =*username = [Defaults stringforkey:@ "username"]; float textSize = [Defaults floatforkey:@ "text_size"= [defaults Boolforkey:@ "auto_login"];
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;
Nskeyedarchiver 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 nscoding the Protocol with 2 methods: Encodewithcoder: This method is called every time an object is archived. In this method, you typically specify how to archive each instance variable in an object, you can use the Encodeobject:forkey: method to archive instance variables Initwithcoder: 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 • Archive a Nsarray object to Documents/array.archive
Nsarray *array = [Nsarray arraywithobjects:@ "a", @ "B", nil];[ Nskeyedarchiver Archiverootobject:array Tofile:path];
• Restore (decode) Nsarray objects
Nsarray *array = [Nskeyedunarchiver Unarchiveobjectwithfile:path];
Dark Horse programmer _ios Application Data Storage-Learning Summary