@@ -227,25 +227,34 @@ - (BFTask *)_cleanupDiskCacheWithRequiredFreeSize:(NSUInteger)requiredSize {
227227 return [BFTask taskFromExecutor: [BFExecutor defaultExecutor ] withBlock: ^id {
228228 NSUInteger size = requiredSize;
229229
230- NSMutableDictionary *commandSizes = [NSMutableDictionary dictionary ];
230+ NSMutableDictionary < NSString *, NSNumber *> *commandSizes = [NSMutableDictionary dictionary ];
231231
232232 [[PFMultiProcessFileLockController sharedController ] beginLockedContentAccessForFileAtPath: self .diskCachePath];
233- NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager ] enumeratorAtPath: self .diskCachePath];
234-
235- NSString *identifier = nil ;
236- while ((identifier = [enumerator nextObject ])) {
237- NSNumber *fileSize = enumerator.fileAttributes [NSFileSize ];
238- if (fileSize) {
239- commandSizes[identifier] = fileSize;
240- size += fileSize.unsignedIntegerValue ;
233+
234+ NSDictionary *directoryAttributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: self .diskCachePath error: nil ];
235+ if ([directoryAttributes[NSFileSize ] unsignedLongLongValue ] > self.diskCacheSize ) {
236+ NSDirectoryEnumerator <NSURL *> *enumerator = [[NSFileManager defaultManager ] enumeratorAtURL: [NSURL fileURLWithPath: self .diskCachePath]
237+ includingPropertiesForKeys: @[ NSURLFileSizeKey ]
238+ options: NSDirectoryEnumerationSkipsSubdirectoryDescendants
239+ errorHandler: nil ];
240+ NSURL *fileURL = nil ;
241+ while ((fileURL = [enumerator nextObject ])) {
242+ NSNumber *fileSize = nil ;
243+ if (![fileURL getResourceValue: &fileSize forKey: NSURLFileSizeKey error: nil ]) {
244+ continue ;
245+ }
246+ if (fileSize) {
247+ commandSizes[fileURL.path.lastPathComponent] = fileSize;
248+ size += fileSize.unsignedIntegerValue ;
249+ }
241250 }
242251 }
243252
244253 [[PFMultiProcessFileLockController sharedController ] endLockedContentAccessForFileAtPath: self .diskCachePath];
245254
246255 if (size > self.diskCacheSize ) {
247256 // Get identifiers and sort them to remove oldest commands first
248- NSArray *identifiers = [commandSizes.allKeys sortedArrayUsingSelector: @selector (compare: )];
257+ NSArray < NSString *> *identifiers = [commandSizes.allKeys sortedArrayUsingSelector: @selector (compare: )];
249258 for (NSString *identifier in identifiers) @autoreleasepool {
250259 [self _removeFileForCommandWithIdentifier: identifier];
251260 size -= [commandSizes[identifier] unsignedIntegerValue ];
0 commit comments