The project encountered a feature that needed to calculate the size of the sandbox directory.
Looked at the method provided by Nsfilemanager to find a
<span style= "FONT-SIZE:18PX;" >-(Nsdictionary *) Attributesofitematpath: (NSString *) path error: (Nserror *) Error ns_available (10_5, 2_0); </ Span>
After testing that this method can only calculate the size of the file, but not directly calculate the size of the folder;
Found this problem, the solution has only one way, is to recursively facilitate the directory to accumulate all the files;
Originally Momentum Dash wrote half, suddenly found
-(Nsarray *) Subpathsatpath: (NSString *) path;
This method actually returns the relative path of all the files in the subdirectory!!
Ouch I go, so do not write recursion, directly splicing on the relative path, directly to get the absolute path you want!!
In this way, the size of a directory can only be written in one while.
+ (nsstring*) getcachesize{nsdate* begin = [NSDate Date]; nsfilemanager* fm = [Nsfilemanager Defaultmanager]; __block nserror* error = nil; __block Nsuinteger fileSize = 0; Get books Cache Dispatch_sync (queue, ^{nsarray* subfiles = [FM Subpathsatpath:bookheaderpath ([Loginplugin share].u Serid)]; NSLog (@ "subpath =%@", subfiles); for (nsstring* filename in subfiles) {if ([FileName hassuffix:@ "png"]| | [FileName hassuffix:@ "jpg"]) {nsdictionary* DIC = [FM Attributesofitematpath:bookpath ([Loginplugin share].userid,filename) error:&e Rror]; Nsuinteger size = (Error 0:[dic fileSize]); FileSize + = size; } } }); nsstring* cachestring = [NSString stringwithformat:@ "%.1FM", filesize/(1024.0*1024)]; Nstimeinterval time = [begin Timeintervalsincenow]; NSLog (@ "Convenient file time:%lf", time/60.0); return cachestring;}
do not worry about the convenience of too many files will affect efficiency, the system convenient 10M files only cost 0.00025S. It's not absolute, it needs to be debugged by myself.
In my project there is another problem, I want to statistics cache, because I used sdwebimage, so to integrate my own downloaded books and Sdimagecache;
Looked at the code under Sdimagecache, which provides a method:
-(void) Calculatesizewithcompletionblock: (Sdwebimagecalculatesizeblock) Completionblock;
The internal implementation is facilitated in a serial queue, and then through the block callback.
But if there's a return value for the code I wrote above, there's a problem.
Because I can't return a size in the block;
It's good to be happy, and it's a joy to worry about.
Haha, this time the solution is to get the Sdimagecache queue, and then after it executes its own compute task, the synchronous call blocks the main thread, so that you do not need to modify the code
For hair so can pinch? Because this queue is serial, wow haha!!!
Here's how:
+ (nsstring*) getcachesize{nsdate* begin = [NSDate Date]; sdimagecache* Imagecache = [[Sdwebimagemanager Sharedmanager] imagecache]; dispatch_queue_t queue =imagecache. Ioqueue; nsfilemanager* fm = [Nsfilemanager Defaultmanager]; __block nserror* error = nil; __block Nsuinteger fileSize = 0; Get the SD cache [Imagecache calculatesizewithcompletionblock:^ (Nsuinteger filecount, Nsuinteger totalsize) {fileSize + = TotalSize; }]; Get books Cache Dispatch_sync (queue, ^{nsarray* subfiles = [FM Subpathsatpath:bookheaderpath ([Loginplugin share].u Serid)]; NSLog (@ "subpath =%@", subfiles); for (nsstring* filename in subfiles) {if ([FileName hassuffix:@ "png"]| | [FileName hassuffix:@ "jpg"]) {nsdictionary* DIC = [FM Attributesofitematpath:bookpath ([Loginplugin share].userid,filename) error:&e Rror]; Nsuinteger size = (Error 0:[dic fileSize]); FileSize + = size;} } }); nsstring* cachestring = [NSString stringwithformat:@ "%.1FM", filesize/(1024.0*1024)]; Nstimeinterval time = [begin Timeintervalsincenow]; NSLog (@ "Convenient file time:%lf", time/60.0); return cachestring;}
Properly, because it is a class method, it does not provide source code for birds! You just need to replace the test path and OK!
IOS calculates the size of a directory in the sandbox