diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 72a797aa..525e127c 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -71,6 +71,11 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week selector:@selector(cleanDisk) name:UIApplicationWillTerminateNotification object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(backgroundCleanDisk) + name:UIApplicationDidEnterBackgroundNotification + object:nil]; #endif } @@ -402,7 +407,29 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week }); } --(unsigned long long)getSize +- (void)backgroundCleanDisk +{ + UIApplication *application = [UIApplication sharedApplication]; + __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^ + { + // Clean up any unfinished task business by marking where you + // stopped or ending the task outright. + [application endBackgroundTask:bgTask]; + bgTask = UIBackgroundTaskInvalid; + }]; + + // 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]; + + [application endBackgroundTask:bgTask]; + bgTask = UIBackgroundTaskInvalid; + }); +} + +- (unsigned long long)getSize { unsigned long long size = 0; NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath];