IOS Development Guide Chapter 11th Study on attribute list of data persistence
Last Update:2015-09-19
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 16px;">1 overview</span></p></p><p><p><span style="font-size: 16px;">Sandbox directory: a security policy that only allows your app to access the directory and not other APPS.</span></p></p><p><p><span style="font-size: 16px;">Subdirectories: documents used to store very large files or data that needs to be updated very frequently</span></p></p><p><p><span style="font-size: 16px;">Nsarray *documentdirectory=nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask,yes);</span></p></p><p><p><span style="font-size: 16px;">Documentdirectory is an array of only one element, and you need to remove the path</span></p></p><p><p><span style="font-size: 16px;">NSString *mydocpath=[documentdirectory objectatindex:0];</span></p></p><p><p><span style="font-size: 16px;">Library Preferences settings data for the application caches for storing cache files</span></p></p><p><p><span style="font-size: 16px;">TMP temp file directory nsstring *temdirectory=nstemporarydirectory ();</span></p></p><p><p><span style="font-size: 16px;">2 Persistence mode data storage</span></p></p><p><p><span style="font-size: 16px;">Attribute list: is an XML file in which arrays and dictionaries can be converted to and from a property list File.</span></p></p><p><p><span style="font-size: 16px;">Nsarray and Nsdictionary provide a way to read and write property list Files: +array/dictionarywithcontentsoffile:</span></p></p><p><p><span style="font-size: 16px;">-initwithcontentoffile:</span></p></p><p><p><span style="font-size: 16px;">Writetofile:atomically: whether to use auxiliary files</span></p></p><p><p><span style="font-size: medium;"><span style="line-height: 24px;">Get the file code:</span></span></p></p><pre><span style="font-size: 16px;">-(nsstring *<span style="color: #000000;"><span style="color: #000000;">) Applicationdocumentsdirectoryfile {gets The full path to the sandbox documents directory file NSString</span></span>*documentdirectory =<span style="color: #000000;"><span style="color: #000000;">[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES) lastobject]; NSString</span></span>*path = [documentdirectory<span style="color: #ff0000;"><span style="color: #ff0000;">stringbyappendingpathcomponent</span></span>:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">noteslist.plist</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">path;}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) createeditablecopyofdatabaseifneeded { <span style="color: #ff0000;">Nsfilemanager</span> </span></span><span style="color: #ff0000;"><span style="color: #ff0000;">*filemanager =</span></span><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #ff0000;">[nsfilemanager defaultmanager];</span> NSString</span></span>*writabledbpath =<span style="color: #000000;"><span style="color: #000000;">[self applicationdocumentsdirectoryfile]; Gets the file directory to <span style="color: #ff0000;">determine whether the file Exists: fileexistsatpath:</span><span style="color: #ff0000;">BOOL dbexits</span> </span></span><span style="color: #ff0000;"><span style="color: #ff0000;">= [filemanager fileexistsatpath:writabledbpath]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">Dbexits) { <span style="color: #ff0000;">Copy a nsstring if it does not exist</span> </span></span>*defaultdbpath = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">noteslist.plist</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Nserror</span></span>*<span style="color: #000000;"><span style="color: #000000;">error; BOOL Success</span></span>= [filemanager<span style="color: #ff0000;"><span style="color: #ff0000;">Copyitematpath:defaultdbpath Topath:writabledbpath error:&</span></span><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #ff0000;">Error</span>]; Nsassert (success,</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">error writing to file</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); assert function, first argument is false throw exception}}</span></span></span></pre><p><p><span style="font-size: 16px;">Read and Write file code: the Program's insert/delete/query is nothing more than a file operation</span></p></p><pre><span style="font-size: 16px;"><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Insert Note Method</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) Create: (note*<span style="color: #000000;"><span style="color: #000000;">) model{nsstring</span></span>*path =<span style="color: #000000;"><span style="color: #000000;">[self applicationdocumentsdirectoryfile]; Nsmutablearray</span></span>*array =<span style="color: #000000;"><span style="color: #000000;">[[nsmutablearray alloc] initwithcontentsoffile:path]; NSDateFormatter</span></span>*dateformat =<span style="color: #000000;"><span style="color: #000000;">[[nsdateformatter alloc] init]; [dateformat setdateformat:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">YYYY-MM-DD HH:mm:ss</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; <span style="color: #ff0000;">encapsulates the incoming data into the required format for the file nsdictionary</span> </span></span><span style="color: #ff0000;"><span style="color: #ff0000;">* dict = [nsdictionary Dictionarywithobjects:@[[dateformat stringfromdate:model.date],model. content] forkeys:@[@ "date" @ "content"</span></span><span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">]];</span> *<br>Setvalue:forkey:<br>* [array addobject:dict]; [array Writetofile:path atomically:yes]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Delete Note Method</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) Remove: (note*<span style="color: #000000;"><span style="color: #000000;">) model{nsstring</span></span>*path =<span style="color: #000000;"><span style="color: #000000;">[self applicationdocumentsdirectoryfile]; Nsmutablearray</span></span>*array =<span style="color: #000000;"><span style="color: #000000;">[[nsmutablearray alloc] initwithcontentsoffile:path]; </span></span><span style="color: #ff0000;">for <span style="color: #ff0000;">(nsdictionary* dict</span> in</span><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #ff0000;">Array)</span> {nsdateformatter</span></span>*dateformatter =<span style="color: #000000;"><span style="color: #000000;">[[nsdateformatter alloc] init]; [dateformatter setdateformat:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">YYYY-MM-DD HH:mm:ss</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; <span style="color: #ff0000;">is to take value out instead of key comparison</span> </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">note* Note = [[note alloc] init];</span></span>NSDate *date = [dateformatter datefromstring:[dict objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">note.content = [dict objectforkey:@ "content"]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Compare date primary keys are equal</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">([date isEqualToDate:model.date]) {[array removeobject:dict]; [array Writetofile:path atomically:yes]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Modify the Note method</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) Modify: (note*<span style="color: #000000;"><span style="color: #000000;">) model{nsstring</span></span>*path =<span style="color: #000000;"><span style="color: #000000;">[self applicationdocumentsdirectoryfile]; Nsmutablearray</span></span>*array =<span style="color: #000000;"><span style="color: #000000;">[[nsmutablearray alloc] initwithcontentsoffile:path]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(nsdictionary* Dict<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #000000;"><span style="color: #000000;">Array) {nsdateformatter</span></span>*dateformatter =<span style="color: #000000;"><span style="color: #000000;">[[nsdateformatter alloc] init]; [dateformatter setdateformat:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">YYYY-MM-DD HH:mm:ss</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSDate</span></span>*date = [dateformatter datefromstring:[dict objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]]; NSString</span></span>* content = [dict objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">content</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Compare date primary keys are equal</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">([date isEqualToDate:model.date]) { <span style="color: #ff0000;">[dict setvalue:content forkey:</span> </span></span><span style="color: #ff0000;"><span style="color: #ff0000;">@ "content"</span></span><span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">];</span> [array Writetofile:path atomically:yes]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">querying all data methods</span></span>-(nsmutablearray*<span style="color: #000000;"><span style="color: #000000;">) findall{nsstring</span></span>*path =<span style="color: #000000;"><span style="color: #000000;">[self applicationdocumentsdirectoryfile]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[self.listdata removeallobjects];</span></span>Nsmutablearray *listdata =<span style="color: #000000;"><span style="color: #000000;">[[nsmutablearray alloc] init]; Nsmutablearray</span></span>*array =<span style="color: #000000;"><span style="color: #000000;">[[nsmutablearray alloc] initwithcontentsoffile:path]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(nsdictionary* Dict<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #000000;"><span style="color: #000000;">Array) {nsdateformatter</span></span>*dateformatter =<span style="color: #000000;"><span style="color: #000000;">[[nsdateformatter alloc] init]; [dateformatter setdateformat:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">YYYY-MM-DD HH:mm:ss</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Note</span></span>* Note =<span style="color: #000000;"><span style="color: #000000;">[[Note alloc] init]; Note.date</span></span>= [dateformatter Datefromstring:[dict objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]]; Note.content</span></span>= [dict objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">content</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [listData addobject:note]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">listData;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">querying data methods by primary key</span></span>-(note*) findbyid: (note*<span style="color: #000000;"><span style="color: #000000;">) model{nsstring</span></span>*path =<span style="color: #000000;"><span style="color: #000000;">[self applicationdocumentsdirectoryfile]; Nsmutablearray</span></span>*array =<span style="color: #000000;"><span style="color: #000000;">[[nsmutablearray alloc] initwithcontentsoffile:path]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(nsdictionary* Dict<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #000000;"><span style="color: #000000;">Array) {nsdateformatter</span></span>*dateformatter =<span style="color: #000000;"><span style="color: #000000;">[[nsdateformatter alloc] init]; [dateformatter setdateformat:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">YYYY-MM-DD HH:mm:ss</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Note</span></span>* Note =<span style="color: #000000;"><span style="color: #000000;">[[Note alloc] init]; Note.date</span></span>= [dateformatter Datefromstring:[dict objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]]; Note.content</span></span>= [dict objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">content</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Compare date primary key is equal date field is primary key</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">([note.date IsEqualToDate:model.date]) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">note; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">nil;}</span></span></span></pre><p><p></p></p><p><p>IOS Development Guide Chapter 11th Study on attribute list of data persistence</p></p></span>