iOS development-The implementation of the clean cache feature

Source: Internet
Author: User
Tags delete cache

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.

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.

The implementation of the offline caching feature introduced today is mainly divided into the cache file size acquisition, delete cache file implementation.

Get the size of the cache file

Because the cache file exists in the sandbox, we can use the Nsfilemanager API to calculate the size of the cache file.

Calculate a single File size
+ (float) Filesizeatpath: (NSString *) path{  nsfilemanager *filemanager=[nsfilemanager Defaultmanager];   if ([FileManager Fileexistsatpath:path]) {  long size=[filemanager attributesofitematpath:path error: Nil].filesize; return size/1024.0/1024.0; } 0;}          
Calculate Directory Size
+ (float) Foldersizeatpath: (nsstring*) path{Nsfilemanager*filemanager=[nsfilemanager Defaultmanager];float foldersize;  if ([FileManager Fileexistsatpath:path]) { Nsarray*childerfiles=[filemanager Subpathsatpath:path];  for (NSString*filename in Childerfiles) {   NSString *absolutepath=[ Path Stringbyappendingpathcomponent:filename];    Foldersize +=[fileservice Filesizeatpath: Absolutepath];  }//sdwebimage the implementation of the framework's own compute cache Span class= "regexp" >  Foldersize+=[[sdimagecache Sharedimagecache] Getsize]/1024.0/1024.0;  return foldersize;}  return 0;}        
Clean up cache files

Also using the Nsfilemanager API for file operations, the Sdwebimage framework itself implements the cleanup cache operation, which we can call directly.

+ (void) ClearCache: (nsstring*) path{Nsfilemanager*filemanager=[nsfilemanager Defaultmanager];  if ([FileManager Fileexistsatpath:path]) { nsarray *childerfiles=[filemanager Subpathsatpath:path];  For  (nsstring *filename in childerfiles) {   //If necessary, add conditions to filter out files that you do not want to delete   Nsstrin G *absolutepath=[path Stringbyappendingpathcomponent:filename];   [FileManager removeitematpath:absolutepath error:nil];  } } [[Sdimagecache Sharedimagecache] cleandisk];}         

Implementation results:

iOS Development-the implementation of the clean cache feature

Related Article

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.