Skip to content

Commit 1c463ad

Browse files
committed
Using the private fileManager instance when on the ioQueue.
1 parent dd682c9 commit 1c463ad

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

SDWebImage/SDImageCache.m

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,11 @@ - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate image
181181
}
182182

183183
if (data) {
184-
// Can't use defaultManager another thread
185-
NSFileManager *fileManager = [NSFileManager new];
186-
187-
if (![fileManager fileExistsAtPath:_diskCachePath]) {
188-
[fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
184+
if (![_fileManager fileExistsAtPath:_diskCachePath]) {
185+
[_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
189186
}
190187

191-
[fileManager createFileAtPath:[self defaultCachePathForKey:key] contents:data attributes:nil];
188+
[_fileManager createFileAtPath:[self defaultCachePathForKey:key] contents:data attributes:nil];
192189
}
193190
});
194191
}
@@ -318,7 +315,7 @@ - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk {
318315

319316
if (fromDisk) {
320317
dispatch_async(self.ioQueue, ^{
321-
[[NSFileManager defaultManager] removeItemAtPath:[self defaultCachePathForKey:key] error:nil];
318+
[_fileManager removeItemAtPath:[self defaultCachePathForKey:key] error:nil];
322319
});
323320
}
324321
}
@@ -342,11 +339,11 @@ - (void)clearDisk {
342339
- (void)clearDiskOnCompletion:(void (^)())completion
343340
{
344341
dispatch_async(self.ioQueue, ^{
345-
[[NSFileManager defaultManager] removeItemAtPath:self.diskCachePath error:nil];
346-
[[NSFileManager defaultManager] createDirectoryAtPath:self.diskCachePath
347-
withIntermediateDirectories:YES
348-
attributes:nil
349-
error:NULL];
342+
[_fileManager removeItemAtPath:self.diskCachePath error:nil];
343+
[_fileManager createDirectoryAtPath:self.diskCachePath
344+
withIntermediateDirectories:YES
345+
attributes:nil
346+
error:NULL];
350347

351348
if (completion) {
352349
dispatch_async(dispatch_get_main_queue(), ^{
@@ -358,15 +355,14 @@ - (void)clearDiskOnCompletion:(void (^)())completion
358355

359356
- (void)cleanDisk {
360357
dispatch_async(self.ioQueue, ^{
361-
NSFileManager *fileManager = [NSFileManager defaultManager];
362358
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
363359
NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
364360

365361
// This enumerator prefetches useful properties for our cache files.
366-
NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtURL:diskCacheURL
367-
includingPropertiesForKeys:resourceKeys
368-
options:NSDirectoryEnumerationSkipsHiddenFiles
369-
errorHandler:NULL];
362+
NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
363+
includingPropertiesForKeys:resourceKeys
364+
options:NSDirectoryEnumerationSkipsHiddenFiles
365+
errorHandler:NULL];
370366

371367
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.maxCacheAge];
372368
NSMutableDictionary *cacheFiles = [NSMutableDictionary dictionary];
@@ -387,7 +383,7 @@ - (void)cleanDisk {
387383
// Remove files that are older than the expiration date;
388384
NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
389385
if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
390-
[fileManager removeItemAtURL:fileURL error:nil];
386+
[_fileManager removeItemAtURL:fileURL error:nil];
391387
continue;
392388
}
393389

@@ -411,7 +407,7 @@ - (void)cleanDisk {
411407

412408
// Delete files until we fall below our desired cache size.
413409
for (NSURL *fileURL in sortedFiles) {
414-
if ([fileManager removeItemAtURL:fileURL error:nil]) {
410+
if ([_fileManager removeItemAtURL:fileURL error:nil]) {
415411
NSDictionary *resourceValues = cacheFiles[fileURL];
416412
NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
417413
currentCacheSize -= [totalAllocatedSize unsignedIntegerValue];
@@ -472,11 +468,10 @@ - (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, NSUInte
472468
NSUInteger fileCount = 0;
473469
NSUInteger totalSize = 0;
474470

475-
NSFileManager *fileManager = [NSFileManager defaultManager];
476-
NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtURL:diskCacheURL
477-
includingPropertiesForKeys:@[NSFileSize]
478-
options:NSDirectoryEnumerationSkipsHiddenFiles
479-
errorHandler:NULL];
471+
NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
472+
includingPropertiesForKeys:@[NSFileSize]
473+
options:NSDirectoryEnumerationSkipsHiddenFiles
474+
errorHandler:NULL];
480475

481476
for (NSURL *fileURL in fileEnumerator) {
482477
NSNumber *fileSize;

0 commit comments

Comments
 (0)