From fb196ead9bd29341bb37d04e77b508d2e7bf90a6 Mon Sep 17 00:00:00 2001 From: Chang Luo Date: Mon, 29 Jul 2013 14:06:15 -0700 Subject: [PATCH] Added background clean disk to UIApplicationDidEnterBackgroundNotification #306 --- SDWebImage/SDImageCache.m | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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];