How can I effectively clear the File Cache in ios Development ?, Clear cache for ios development

Source: Internet
Author: User

How can I effectively clear the File Cache in ios Development ?, Clear cache for ios development

How can I effectively clear the File Cache in ios development? In some applications, some caches should be stored on the disk, and the application will also provide the clear cache button, which will also tell you how large the cache is.

So how can we get the cache size and how can we clear it? First, we need to make it clear that to get the cache size, we must get the file size.

However, it is worth noting that the folder itself does not have the file size attribute, and there may be questions, but right-click to view it, which is calculated in the folder

. In ios, if we want to manipulate files, we need to use NSFileManager as a class. If we want to directly obtain the folder size

No. The value of NSFileSize is incorrect. However, if the file size is obtained, as shown below:

// Obtain the File Manager NSFileManager * mgr = [NSFileManager defaultManager]; // obtain the cache path NSString * string = @ "file path"; NSDictionary * attrs = [mgr attributesOfItemAtPath: string error: NULL]; NSLog (@ "% @", attrs); // obtain all direct content in the folder NSArray * contents = [mgr contentsOfDirectoryAtPath: string error: NULL];

We can add a category to NSString to calculate the folder size.

 

 

-(NSInteger) fileSize {NSFileManager * mgr = [NSFileManager defaultManager]; // you can determine whether BOOL isDirectory = NO is a flag of the folder. // The isDirectory is passed in, if YES is taken out, it is the folder BOOL exists = [mgr fileExistsAtPath: self isDirectory: & isDirectory]; // you can determine whether a file or folder does not exist if (exists = NO) {return 0;} // if it is a folder, We will traverse all the content in caches including direct and indirect content if (isDirectory) {NSArray * subPaths = [mgr subpathsAtPath: self]; NSInteger totalByteSize = 0; for (NSString * subPath in subPaths) {// obtain the full path NSString * fullSubPath = [self stringByAppendingPathComponent: subPath]; // determine whether it is a file BOOL isDirectory = NO; // indicates that it is a file if (isDirectory = NO) {totalByteSize + = [[mgr attributesOfItemAtPath: fullSubPath error: NULL] [NSFileSize] integerValue] ;}return totalByteSize ;}// if it is a single file, the output file size else {return [[mgr attributesOfItemAtPath: self error: NULL] [NSFileSize] integerValue] ;}}
If we want to delete a file or a folder, we can simply use a method of the file manager object.
NSString * str1 = @ "file/folder path"; [mgr removeItemAtPath: str1 error: NULL];

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.