Three20 learning to clear the network Cache

Source: Internet
Author: User

Using the Three20 framework will produce a lot of cache files, which are stored in the application's caches/Three20/folder. After a long time, it will occupy a lot of storage space. In this case, you need to provide an entry to clear the cache. For example, on the setting page, we need to display the cache size before deletion, prompt whether the current cache needs to be cleared: the method for calculating the cache size is as follows: [cpp] dispatch_async (dispatch_get_global_queue (queue, 0), ^ (void) {NSArray * paths = Queue (NSCachesDirectory, NSUserDomainMask, YES); NSString * path = [[paths objectAtIndex: 0] stringByAppendingPathComponent: @ "/Three20/"]; NSFileManager * fileManager = [NSFileMana Ger defaultManager]; if ([fileManager fileExistsAtPath: path]) {NSEnumerator * childEnumber = [[fileManager subpathsOfDirectoryAtPath: path error: nil] objectEnumerator]; unsigned long folderSize = 0; NSString * fileName; while (fileName = [childEnumber nextObject])! = Nil) {NSString * childFilePath = [path stringByAppendingPathComponent: fileName]; folderSize + = [fileManager attributesOfItemAtPath: childFilePath error: nil]. fileSize;} dispatch_async (dispatch_get_main_queue (), ^ (void) {abstrachebutton. text = [NSString stringWithFormat: @ "clear image cache (% 0.1f Mb)", (float) folderSize/1000/1000]; [self reloadTableCells] ;}}); dispatch_async (dispatch_get_global_queue (D Dependencies, 0), ^ (void) {NSArray * paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES); NSString * path = [[paths objectAtIndex: 0] stringByAppendingPathComponent: @ "/Three20/"]; NSFileManager * fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath: path]) {NSEnumerator * childEnumber = [[fileManager subpathsOfDirectoryAtPat H: path error: nil] objectEnumerator]; unsigned long folderSize = 0; NSString * fileName; while (fileName = [childEnumber nextObject])! = Nil) {NSString * childFilePath = [path stringByAppendingPathComponent: fileName]; folderSize + = [fileManager attributesOfItemAtPath: childFilePath error: nil]. fileSize;} dispatch_async (dispatch_get_main_queue (), ^ (void) {abstrachebutton. text = [NSString stringWithFormat: @ "clear image cache (% 0.1f Mb)", (float) folderSize/1000/1000]; [self reloadTableCells] ;}}); for details about how to use GCD, refer to Grand Central Dispatch (G CD) It takes some time to calculate the file size when there are too many cached files. How to clear the cache: [cpp] [[TTURLCache sharedCache] removeAll: YES]; [[TTURLCache sharedCache] removeAll: YES];

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.