IOS cache file Cleanup implementation code _ios

Source: Internet
Author: User

Mobile Internet app development, must always pay attention to the user experience, so as to avoid the app or mobile phone and other mobile device card death, the following is the processing of cached files.

Mobile applications in processing network resources, generally do offline cache processing, which is the most typical image caching, which is very popular offline caching framework for sdwebimage.

However, offline caching consumes cell phone storage space, so caching cleanup is basically the standard feature of information, shopping, and reading apps.

The implementation of off-line caching, which is introduced today, is mainly divided into the acquisition of cache file size and the realization of clearing cache file.

1. Get the size of the cached file

-(float) readcachesize {nsstring *cachepath = [Nssearchpathfordirectoriesindomains (Nscachesdirectory, NSUserDomainM
  Ask, YES) firstobject];
return [self foldersizeatpath:cachepath];
Because the cache file is in a sandbox, we can compute the size of the cached file through the Nsfilemanager API. Traverse folder to get folder size, return how many M-(float) Foldersizeatpath: (
  NSString *) folderpath{Nsfilemanager * manager = [Nsfilemanager Defaultmanager]; if (![
  Manager Fileexistsatpath:folderpath]) return 0;
  Nsenumerator *childfilesenumerator = [[manager Subpathsatpath:folderpath] objectenumerator];
  NSString * FILENAME;
  Long long foldersize = 0; while (fileName = [Childfilesenumerator nextobject])!= nil) {//Get file Full path NSString * Fileabsolutepath = [Folderpat
    H Stringbyappendingpathcomponent:filename];
  Foldersize + = [self filesizeatpath:fileabsolutepath];
  
Return foldersize/(1024.0 * 1024.0); //Calculates the size of a single file-(Long Long) Filesizeatpath: ( NSString *) filepath{Nsfilemanager * manager = [Nsfilemanager DefaultmaNager];
  if ([manager Fileexistsatpath:filepath]) {return [[manager Attributesofitematpath:filepath Error:nil] fileSize];
return 0;

 

 }

2. Clear Cache

-(void) clearfile
{
  NSString * CachePath = [Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) firstobject];
  Nsarray * files = [[Nsfilemanager Defaultmanager] subpathsatpath:cachepath];
  NSLog (@ "Cachpath =%@", CachePath);
  For (NSString * p in files) {
    
    Nserror * error = nil;
    Get file Full path
    nsstring * Fileabsolutepath = [CachePath stringbyappendingpathcomponent:p];
    
    if ([[[Nsfilemanager Defaultmanager] Fileexistsatpath:fileabsolutepath]) {
      [[Nsfilemanager Defaultmanager] Removeitematpath:fileabsolutepath error:&error];
    }
  }
  
  Read cache size
  float cacheSize = [self readcachesize] *1024;
  Self.cacheSize.text = [NSString stringwithformat:@ "%.2fkb", cacheSize];

}


 

Thank you for reading, I hope to help you, thank you for your support for this site!

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.