Implementation of the iOS cache cleanup idea
Last Update:2015-09-14
Source: Internet
Author: User
<span id="Label3"></p><p><p>first, talk about a few knowledge points about file management before you talk about cache Cleanup.</p></p><p><p><span style="font-size: 20px; background-color: rgb(0, 176, 80);">the first part of the file management several common methods</span></p></p><p><p><span style="font-size: 14px;">Compare the contents of a two file</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">/*-contentsequalatpath:andpath:does not take to account data stored in the resource fork or filesystem extended attrib Utes.</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">*/</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">-(<span style="color: #bb2ca2">BOOL</span>) contentsequalatpath: (<span style="color: #703daa">nsstring</span> *) path1 andpath: (<span style="color: #703daa">nsstring</span> *) path2;</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p><p><span style="font-size: 14px; background-color: rgb(255, 0, 0);">Whether the files in the two paths exist</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">/* The following methods is of limited utility. Attempting to predicate behavior based in the current state of the filesystem or a particular file on the filesystem is En Couraging odd behavior in the face of filesystem race Conditions. It's far better to attempt an operation (like loading a file or creating a Directory) and handle the error gracefully than It is-to-try-to-figure out ahead of time whether the operation would succeed.</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">*/</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">-(<span style="color: #bb2ca2">BOOL</span>) fileexistsatpath: (<span style="color: #703daa">nsstring</span> *) path;</p></p><p><p><span style="font-size: 14px;"><br></span></p></p><p><p><span style="font-size: 14px; background-color: rgb(255, 0, 0);">Three removal of file contents in a path</span><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">/* These methods replace their non-error returning counterparts BELOW. See the Nsfilemanagerdelegate protocol below for methods that is dispatched to the Nsfilemanager instance ' s Delegate.</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">*/</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">-(<span style="color: #bb2ca2">BOOL</span>) removeitematpath: (<span style="color: #703daa">nsstring</span> *) path error: (<span style="color: #703daa">nserror</span> * *) Error</p></p><p><p><span style="font-size: 12.5px;"><br></span></p></p><p><p><span style="font-size: 14px;">Four read data from a file</span><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">/* These methods is provided here for Compatibility. The corresponding methods on NSData which return nserrors should is regarded as the primary method of creating a file from An nsdata or retrieving the contents of a, file as an Nsdata.</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">*/</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">-(<span style="color: #703daa">nsdata</span> *) contentsatpath: (<span style="color: #703daa">nsstring</span> *) path;</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-size: 14px; background-color: rgb(255, 0, 0);">Five remove the cache file contents from the cache path, The return value is an array, receive the array</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">/* Subpathsatpath:returns An Nsarray of all contents and subpaths recursively from the provided path. This is very expensive to compute for deep filesystem hierarchies, and should probably is Avoided.</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">*/</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">-(<span style="color: #703daa">nsarray</span> *) subpathsatpath: (<span style="color: #703daa">nsstring</span> *) path;</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p><p><span style="font-size: 14px; background-color: rgb(255, 0, 0);">Six based on the file name stitching path</span><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">-(<span style="color: #703daa">nsstring</span> *) stringbyappendingpathcomponent: (<span style="color: #703daa">nsstring</span> *) str;</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-size: 14px;">VII calculates the size of the file in the path according to the path</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #bb2ca2">Long</span> <span style="color: #000000"> </span> <span style="color: #bb2ca2">long</span> <span style="color: #000000"> size=[filemanager </span>attributesofitematpath<span style="color: #000000">:p ath </span>error<span style="color: #000000">:</span><span style="color: #bb2ca2">Nil </span><span style="color: #000000">].</span> FileSize<span style="color: #000000">;</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000"><br></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000"><br></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000"><span style="color: rgb(0, 0, 0); background-color: rgb(0, 176, 80); font-size: 20px;">the second part of the text description of the cache cleanup idea</span></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000"><br></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-size: 14px;">The first step is to get the cache file path that needs to be cleaned</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000"> </span> <span style="color: #703daa">nsstring</span><span style="color: #000000">* filePath =[</span>nssearchpathfordirectoriesindomains<span style="color: #000000">(</span>nscachesdirectory <span style="color: #000000">, </span> Nsuserdomainmask<span style="color: #000000">, </span> <span style="color: #bb2ca2">YES</span><span style="color: #000000">) </span>lastobject<span style="color: #000000">];</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000"><br></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-size: 14px;">The second step is to create the file Manager</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="color: #000000"> </span>nsfilemanager<span style="color: #000000"> *filemanager=[</span>nsfilemanager<span style="color: #000000"> </span> <span style="color: #3d1d81">defaultmanager</span><span style="color: #000000">];</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-size: 14px;">The third step is to determine if the file contents in the cache file path exist</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="color: #bb2ca2">if</span> ([filemanager <span style="color: #3d1d81">fileexistsatpath</span>:p ath])</p></p><p><p><span style="font-size: 14px;">Fourth step through the cache path, get all the files in the path array</span><br></p></p><p><p><span style="font-size: 12.5px;"></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="color: #703daa">nsarray</span> *childerfiles = [filemanager <span style="color: #3d1d81">subpathsatpath</span>:p ath];</p></p><p><p><span style="font-size: 14px;">The fifth step iterates through the array, stitching the array</span><br></p></p><p><p><span style="font-size: 12.5px;"></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="color: #703daa">nsstring</span> *absolutepath=[path <span style="color: #3d1d81">stringbyappendingpathcomponent</span>: fileName];</p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-size: 14px;">Sixth step calculating File size</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #bb2ca2">Long</span> <span style="color: #000000"> </span> <span style="color: #bb2ca2">long</span> <span style="color: #000000"> size=[filemanager </span>attributesofitematpath<span style="color: #000000">:p ath </span>error<span style="color: #000000">:</span><span style="color: #bb2ca2">Nil </span><span style="color: #000000">].</span> FileSize<span style="color: #000000">;</span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000"><br></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000">the <span style="color: rgb(0, 0, 0); font-size: 20px; background-color: rgb(0, 176, 80);">third part of the cache cleaning ideas Flowchart</span></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000"><span style="color: rgb(0, 0, 0); font-size: 18px; background-color: rgb(0, 176, 80);"><br></span></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000"><span style="color: rgb(0, 0, 0); font-size: 18px; background-color: rgb(0, 176, 80);"></span></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br></p></p><br><p><p><br></p></p><p><p>Implementation of the iOS cache cleanup idea</p></p></span>