iOS development-the implementation of the clean cache feature

Source: Internet
Author: User
<span id="Label3"></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">Mobile applications in the processing of network resources, generally do offline cache processing, which is the most typical picture cache, where the popular offline cache framework is Sdwebimage.</p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">however, offline caching takes up storage space on your phone, so the cache cleanup function is basically a standard feature for information, shopping, and reading Apps.</p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">The implementation of the offline caching feature introduced today is mainly divided into the cache file size acquisition, Delete cache file Implementation.</p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px"><span style="margin:0px; padding:0px; font-size:16px">Get the size of the cache file</span></span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">Because the cache file exists in the sandbox, we can use the Nsfilemanager API to calculate the size of the cache File.</p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px">Calculate a single File size</span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><pre style="margin-top:0px; margin-bottom:0px; padding:0px; white-space:pre-wrap; word-wrap:break-word; font-family:‘Courier New‘!important">+(<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">float</span></span>) Filesizeatpath: (nsstring *<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">) Path{nsfilemanager</span></span>*filemanager=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[nsfilemanager defaultmanager]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">if</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">([filemanager fileexistsatpath:path]) {</span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">Long</span></span> <span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">Long</span></span>Size=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[filemanager Attributesofitematpath:path error:nil].filesize; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">return</span></span>size/<span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">1024.0</span></span>/<span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">1024.0</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">; } </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">return</span></span> <span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">0</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">;}</span></span></pre><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px">Calculate Directory Size</span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><pre style="font-family:‘Courier New‘!important; margin-top:0px; margin-bottom:0px; padding:0px; white-space:pre-wrap; word-wrap:break-word">+(<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">float</span></span>) Foldersizeatpath: (nsstring *<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">) Path{nsfilemanager</span></span>*filemanager=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[nsfilemanager defaultmanager]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">float</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">foldersize; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">if</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">([filemanager fileexistsatpath:path]) {nsarray</span></span>*childerfiles=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[filemanager subpathsatpath:path]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"></span> for</span>(nsstring *filename<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">inch</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">childerfiles) {nsstring</span></span>*absolutepath=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[path stringbyappendingpathcomponent:filename]; Foldersize</span></span>+=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[fileservice filesizeatpath:absolutepath]; }<br style="margin:0px; padding:0px">     <span style="margin:0px; padding:0px; color:rgb(51,153,102); line-height:1.5!important">implementation of The//sdwebimage Framework's own compute cache</span> foldersize</span></span>+=[[sdimagecache sharedimagecache] getsize]/<span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">1024.0</span></span>/<span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">1024.0</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">return</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">foldersize; } </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">return</span></span> <span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">0</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">;}</span></span></pre><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><br></span></span><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><br></span></span><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span style="font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">implementation Results:</span></span></span><span style="font-family:verdana,courier new"><span style="font-family:verdana,courier new"><span style="font-size:14px; line-height:23px"><u><br></u></span></span></span><span style="font-family:verdana,courier new"><span style="font-family:verdana,courier new"><span style="font-size:14px; line-height:23px"><u><br></u></span></span></span><span class="cnblogs_code_copy" style="font-family:‘Courier New‘!important; margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="font-family:‘Courier New‘!important; margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica"><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica"></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px"><span style="margin:0px; padding:0px; font-size:16px">Clean up cache files</span></span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px; font-size:13px"><span style="margin:0px; padding:0px; font-size:14px"><span style="margin:0px; padding:0px; line-height:24px">Also using the Nsfilemanager API for file operations, The Sdwebimage framework itself implements the cleanup cache operation, which we can call Directly.</span></span></span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><pre style="margin-top:0px; margin-bottom:0px; padding:0px; white-space:pre-wrap; word-wrap:break-word; font-family:‘Courier New‘!important">+(<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">void</span></span>) clearcache: (nsstring *<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">) Path{nsfilemanager</span></span>*filemanager=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[nsfilemanager defaultmanager]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">if</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">([filemanager fileexistsatpath:path]) {nsarray</span></span>*childerfiles=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[filemanager subpathsatpath:path]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"></span> for</span>(nsstring *filename<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">inch</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">childerfiles) { </span></span><span style="margin:0px; padding:0px; color:rgb(0,128,0); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,128,0); line-height:1.5!important">//</span></span><span style="margin:0px; padding:0px; color:rgb(0,128,0); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,128,0); line-height:1.5!important">if necessary, add conditions to filter out files that you do not want to delete</span></span>NSString *absolutepath=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[path stringbyappendingpathcomponent:filename]; [filemanager Removeitematpath:absolutepath error:nil]; }} [[sdimagecache sharedimagecache] cleandisk];}</span></span></pre> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p> iOS development-the implementation of the clean cache feature </p> </p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.