Authored by 卢克

It's generally a bad idea to remove items from a container while iterating throu…

…gh it. And fix the thread safe issue.
... ... @@ -358,7 +358,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
}
- (void)cleanDiskWithCompletionBlock:(void (^)())completionBlock {
dispatch_async(self.ioQueue, ^{
dispatch_barrier_async(self.ioQueue, ^{
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
... ... @@ -376,6 +376,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
//
// 1. Removing files that are older than the expiration date.
// 2. Storing file attributes for the size-based cleanup pass.
NSMutableArray *urlsToDelete = [[NSMutableArray alloc] init];
for (NSURL *fileURL in fileEnumerator) {
NSDictionary *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:NULL];
... ... @@ -387,7 +388,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
// Remove files that are older than the expiration date;
NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
[_fileManager removeItemAtURL:fileURL error:nil];
[urlsToDelete addObject:fileURL];
continue;
}
... ... @@ -396,6 +397,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
currentCacheSize += [totalAllocatedSize unsignedIntegerValue];
[cacheFiles setObject:resourceValues forKey:fileURL];
}
for (NSURL *fileURL in urlsToDelete) {
[_fileManager removeItemAtURL:fileURL error:nil];
}
// If our remaining disk cache exceeds a configured maximum size, perform a second
// size-based cleanup pass. We delete the oldest files first.
... ...