Data persistence means that data is written to a folder and stored in a local sandbox. The so-called sandbox is a folder, which is used to store things.
Sandbox mechanism: 1. When the app is installed on the phone, the phone system assigns a sandbox folder to the app
2. Each application can only access its own sandbox folder and cannot access other programs ' folders
3. Applications can obtain certain rights to access the system's folders
4. The essence of the sandbox is a folder, the name is randomly assigned
Sandbox path in simulator:
Finder-> to (+ ALT) Resource library ->application support-> iPhone simulator-> simulator version -& Gt all content of an app application-> sandbox
four folders under Sandbox files: Document: Store user data, information that needs to be backed up, such as itunes sync, iCloud so audio video don't put it here
Library/caches cache files, audio and video novels, etc.
Library/pererences (does not directly manipulate it)
. App (folder) (does not directly manipulate it, usually only read, with NSBundle) such as dragged into the project Plilst photos
TMP store temporary files, such as: Download the zip package, unzip and put caches and then delete
Now that the sandbox is finished, it's time to talk about data persistence.
Simple data reading and writing for data persistence
/*
Simple object: NSString nsarray nsdictionary NSData
Data persistence: Writing data to a file and storing it in a sandbox folder
File read/write
Write: Build file [Object Write] WriteToFile
Read: Initialization method reads the contents of the file Initwithcontentsoffile
Note: Object types stored in arrays and dictionaries must be of the above four objects!!!!
NSData is used to store binary data such as picture-to-NSData and write
*/
1. Using Nsuserdefaults to operate a plist file
2.plist files do not need to be created by the developer, you do not need to specify a path, the default path is under the Preferences folder
Nsuserdefaults * Userdefaults = [Nsuserdefaultsstandarduserdefaults];
[Userdefaults setobject:@ "Huang Rong" forkey:@ "name"];
[Userdefaults setinteger:22 forkey:@ "age"];
//immediate (synchronous) storage, otherwise there is a delay in storage
[Userdefaults Synchronize];
Archiving (serialization) is also used to achieve data persistence, not data persistence, but two sequential steps
Complex objects (Basic data Type objects must follow the Nscoding protocol, and attributes in object types must also be respected)->nsdata
Person-> Archive->nsdata->writetofile->document
Person <-back to archive <-nsdata <-initwithcontentsoffile <-document
1. Archive: serialization, encoding, similar to compressed files.
2. Archive: Convert eligible objects to NSData objects
3. Objects that can be archived must comply with the Nscoding agreement. object, and if it is an object, it must also comply with the Nscoding protocol
Files in NSBundle are read-only and not writable
Nsfilemanager File Manager, single-instance get objects, manipulate files, create, copy, cut, delete; Create sub-folders; Determine if the file exists and the return value is bool
Archive
Use tools to archive person objects
Nsmutabledata is a container, first empty, archived, and used to put the person
Nsmutabledata * Archivedata = [Nsmutabledatadata];
//Archive Tool, inherited from Nscoder
Nskeyedarchiver * archiver = [[Nskeyedarchiveralloc] initforwritingwithmutabledata:archivedata];
To archive a Person object
[Archiver encodeobject:p forkey:@ "person"];
//End archive Operation
[Archiver finishencoding];
NSLog (@ "archivedata =%@", archivedata);
//Data persistence storing archived data in a sandbox
NSString * Documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject ];
NSString * FilePath = [documentpath stringbyappendingpathcomponent:@ "Singleperson"];
NSLog (@ "FilePath =%@", FilePath);
[Archivedata Writetofile:filepath Atomically:yes];
[P release];
[Archiver release];
Anti-archiving
NSString * documentpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, Nsuserdomainmask, YES) lastobject];
nsstring * FilePath = [documentpath stringbyappendingpathcomponent:@ " Singleperson "];
//Read data from a sandbox file
nsdata * readdata = [nsdatadatawithcontentsoffile: FilePath];
//Use the tool to archive data back to the person object
Nskeyedunarchiver * Unarchiver = [[Nskeyedunarchiveralloc] initforreadingwithdata:readdata];
//decoding
person * p = [unarchiver decodeobjectforkey:@ ' person '];
[Unarchiver finishdecoding];
NSLog (@ "name =%@ age =%ld", p.name,p.age);
[Unarchiver release];