Authored by Olivier Poitrey

Merge pull request #727 from kejinlu/master

It's generally a bad idea to remove items from a container while iterating through it.
... ... @@ -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;
}
... ... @@ -397,6 +398,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
[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.
if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize) {
... ...