Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions Parse/Internal/PFCommandCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,34 @@ - (BFTask *)_cleanupDiskCacheWithRequiredFreeSize:(NSUInteger)requiredSize {
return [BFTask taskFromExecutor:[BFExecutor defaultExecutor] withBlock:^id{
NSUInteger size = requiredSize;

NSMutableDictionary *commandSizes = [NSMutableDictionary dictionary];
NSMutableDictionary<NSString *, NSNumber *> *commandSizes = [NSMutableDictionary dictionary];

[[PFMultiProcessFileLockController sharedController] beginLockedContentAccessForFileAtPath:self.diskCachePath];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath];

NSString *identifier = nil;
while ((identifier = [enumerator nextObject])) {
NSNumber *fileSize = enumerator.fileAttributes[NSFileSize];
if (fileSize) {
commandSizes[identifier] = fileSize;
size += fileSize.unsignedIntegerValue;

NSDictionary *directoryAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self.diskCachePath error:nil];
if ([directoryAttributes[NSFileSize] unsignedLongLongValue] > self.diskCacheSize) {
NSDirectoryEnumerator<NSURL *> *enumerator = [[NSFileManager defaultManager] enumeratorAtURL:[NSURL fileURLWithPath:self.diskCachePath]
includingPropertiesForKeys:@[ NSURLFileSizeKey ]
options:NSDirectoryEnumerationSkipsSubdirectoryDescendants
errorHandler:nil];
NSURL *fileURL = nil;
while ((fileURL = [enumerator nextObject])) {
NSNumber *fileSize = nil;
if (![fileURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:nil]) {
continue;
}
if (fileSize) {
commandSizes[fileURL.path.lastPathComponent] = fileSize;
size += fileSize.unsignedIntegerValue;
}
}
}

[[PFMultiProcessFileLockController sharedController] endLockedContentAccessForFileAtPath:self.diskCachePath];

if (size > self.diskCacheSize) {
// Get identifiers and sort them to remove oldest commands first
NSArray *identifiers = [commandSizes.allKeys sortedArrayUsingSelector:@selector(compare:)];
NSArray<NSString *> *identifiers = [commandSizes.allKeys sortedArrayUsingSelector:@selector(compare:)];
for (NSString *identifier in identifiers) @autoreleasepool {
[self _removeFileForCommandWithIdentifier:identifier];
size -= [commandSizes[identifier] unsignedIntegerValue];
Expand Down