From 87aed007335603587d0c7d0fb09a9dc9235a67a1 Mon Sep 17 00:00:00 2001 From: Matej Bukovinski Date: Mon, 24 Mar 2014 21:14:21 +0100 Subject: [PATCH] Fixed the background task handled in backgroundCleanDisk. - the background task had no effect, since cleanDisk returns immediately and thereby cancels the background task - adding cleanDiskWithCompletionBlock: and modifying backgroundCleanDisk to use this method resolves the issue --- SDWebImage/SDImageCache.m | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 2d4471f..3f36e5e 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -354,6 +354,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { } - (void)cleanDisk { + [self cleanDiskWithCompletionBlock:nil]; +} + +- (void)cleanDiskWithCompletionBlock:(void (^)())completionBlock { dispatch_async(self.ioQueue, ^{ NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES]; NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey]; @@ -418,6 +422,11 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { } } } + if (completionBlock) { + dispatch_async(dispatch_get_main_queue(), ^{ + completionBlock(); + }); + } }); } @@ -431,13 +440,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { }]; // Start the long-running task and return immediately. - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - // Do the work associated with the task, preferably in chunks. - [self cleanDisk]; - + [self cleanDiskWithCompletionBlock:^{ [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; - }); + }]; } - (NSUInteger)getSize {