Merge pull request #434 from jparise/calculate-size

Add -calculateSizeWithCompletionBlock:.
This commit is contained in:
Olivier Poitrey 2013-06-25 23:50:31 -07:00
commit 8a95ce759c
2 changed files with 38 additions and 0 deletions

View File

@ -154,4 +154,9 @@ typedef enum SDImageCacheType SDImageCacheType;
*/
- (int)getDiskCount;
/**
* Asynchronously calculate the disk cache's size.
*/
- (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, unsigned long long totalSize))completionBlock;
@end

View File

@ -423,4 +423,37 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
return count;
}
- (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, unsigned long long totalSize))completionBlock
{
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
dispatch_async(self.ioQueue, ^
{
NSUInteger fileCount = 0;
unsigned long long totalSize = 0;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtURL:diskCacheURL
includingPropertiesForKeys:@[ NSFileSize ]
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:NULL];
for (NSURL *fileURL in fileEnumerator)
{
NSNumber *fileSize;
[fileURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:NULL];
totalSize += [fileSize unsignedLongLongValue];
fileCount += 1;
}
if (completionBlock)
{
dispatch_async(dispatch_get_main_queue(), ^
{
completionBlock(fileCount, totalSize);
});
}
});
}
@end