Authored by Matej Bukovinski

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
@@ -354,6 +354,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { @@ -354,6 +354,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
354 } 354 }
355 355
356 - (void)cleanDisk { 356 - (void)cleanDisk {
  357 + [self cleanDiskWithCompletionBlock:nil];
  358 +}
  359 +
  360 +- (void)cleanDiskWithCompletionBlock:(void (^)())completionBlock {
357 dispatch_async(self.ioQueue, ^{ 361 dispatch_async(self.ioQueue, ^{
358 NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES]; 362 NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
359 NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey]; 363 NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
@@ -418,6 +422,11 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { @@ -418,6 +422,11 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
418 } 422 }
419 } 423 }
420 } 424 }
  425 + if (completionBlock) {
  426 + dispatch_async(dispatch_get_main_queue(), ^{
  427 + completionBlock();
  428 + });
  429 + }
421 }); 430 });
422 } 431 }
423 432
@@ -431,13 +440,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { @@ -431,13 +440,10 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
431 }]; 440 }];
432 441
433 // Start the long-running task and return immediately. 442 // Start the long-running task and return immediately.
434 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
435 - // Do the work associated with the task, preferably in chunks.  
436 - [self cleanDisk];  
437 - 443 + [self cleanDiskWithCompletionBlock:^{
438 [application endBackgroundTask:bgTask]; 444 [application endBackgroundTask:bgTask];
439 bgTask = UIBackgroundTaskInvalid; 445 bgTask = UIBackgroundTaskInvalid;
440 - }); 446 + }];
441 } 447 }
442 448
443 - (NSUInteger)getSize { 449 - (NSUInteger)getSize {