Several ways to clean up the cache in iOS

Source: Internet
Author: User

iOS cleans up the cache in several ways, with the need for friends to refer to the following:

1. Calculate File Size:

-(Long Long) Filesizeatpath: (nsstring*) filepath{

nsfilemanager* manager = [Nsfilemanager Defaultmanager];

if ([manager Fileexistsatpath:filepath]) {

return [[Manager Attributesofitematpath:filepath Error:nil] fileSize];

}

return 0;

}

2. Calculate the folder size:

//Traversal folder to get the 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) {

nsstring* Fileabsolutepath = [FolderPath stringbyappendingpathcomponent:filename];

Foldersize + = [self filesizeatpath:fileabsolutepath];

}

Return foldersize/(1024.0*1024.0);

}

3. Clear the first type of cache

-(void) Action: (ID) sender

{

Completely clear the cache the first method

UIButton * button = sender;

[Button settitle:@ "clean up" forstate:uicontrolstatenormal];

Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES);

NSString *path = [Paths lastobject];



NSString *str = [nsstring stringwithformat:@ "cache cleared%.1FM", [self foldersizeatpath:path]];

NSLog (@ "%@", str);

Nsarray *files = [[Nsfilemanager Defaultmanager] subpathsatpath:path];

For (NSString *p in files) {

Nserror *error;

NSString *path = [Path stringbyappendingpathcomponent:p];

if ([[[Nsfilemanager Defaultmanager] Fileexistsatpath:path]) {

[[Nsfilemanager Defaultmanager] Removeitematpath:path error:&error];

}

}

4.SDImage third-party methods of clearing the cache: (Generally, the cache of images is cleared)

[[Sdimagecache Sharedimagecache] cleardisk]; Clear Disk Cache

[[Sdimagecache Sharedimagecache] clearmemory]; Clear Memory Cache

5. Turn on multithreading to clear the cache:


Dispatch_async (

Dispatch_get_global_queue (dispatch_queue_priority_default, 0)

, ^{

NSString *cachpath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) objectAtIndex:0] ;

NSLog (@ "%@", Cachpath);

Nsarray *files = [[Nsfilemanager Defaultmanager] subpathsatpath:cachpath];

NSLog (@ "Files:%d", [Files Count]);

For (NSString *p in files) {

Nserror *error;

NSString *path = [Cachpath stringbyappendingpathcomponent:p];

if ([[[Nsfilemanager Defaultmanager] Fileexistsatpath:path]) {

[[Nsfilemanager Defaultmanager] Removeitematpath:path error:&error];

}

}

[Self Performselectoronmainthread: @selector (clearcachesuccess) Withobject:nil waituntildone:yes];});

}

-(void) clearcachesuccess

{

NSLog (@ "clean success");

}

Several ways to clean up the cache in iOS

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.