Skip to content

Commit

Permalink
makes potentially long-running purge methods run as background tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
artgillespie committed Aug 22, 2013
1 parent 9e9d6fe commit fcdeb2c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions OGImage/OGImageCache.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -122,10 +122,15 @@ - (void)setImage:(__OGImage *)image forKey:(NSString *)key {


- (void)purgeCache:(BOOL)wait { - (void)purgeCache:(BOOL)wait {
[_memoryCache removeAllObjects]; [_memoryCache removeAllObjects];
UIBackgroundTaskIdentifier taskId = UIBackgroundTaskInvalid;
taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:taskId];
}];
void (^purgeFilesBlock)(void) = ^{ void (^purgeFilesBlock)(void) = ^{
for (NSURL *url in [[NSFileManager defaultManager] enumeratorAtURL:OGImageCacheURL() includingPropertiesForKeys:nil options:0 errorHandler:nil]) { for (NSURL *url in [[NSFileManager defaultManager] enumeratorAtURL:OGImageCacheURL() includingPropertiesForKeys:nil options:0 errorHandler:nil]) {
[[NSFileManager defaultManager] removeItemAtURL:url error:nil]; [[NSFileManager defaultManager] removeItemAtURL:url error:nil];
} }
[[UIApplication sharedApplication] endBackgroundTask:taskId];
}; };
if (YES == wait) { if (YES == wait) {
dispatch_sync(_cacheFileTasksQueue, purgeFilesBlock); dispatch_sync(_cacheFileTasksQueue, purgeFilesBlock);
Expand Down Expand Up @@ -159,6 +164,10 @@ - (void)purgeMemoryCacheForKey:(NSString *)key andWait:(BOOL)wait {
} }


- (void)purgeDiskCacheOfImagesLastAccessedBefore:(NSDate *)date { - (void)purgeDiskCacheOfImagesLastAccessedBefore:(NSDate *)date {
UIBackgroundTaskIdentifier taskId = UIBackgroundTaskInvalid;
taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:taskId];
}];
dispatch_async(_cacheFileTasksQueue, ^{ dispatch_async(_cacheFileTasksQueue, ^{
NSURL *cacheURL = OGImageCacheURL(); NSURL *cacheURL = OGImageCacheURL();
for (NSURL *fileURL in [[NSFileManager defaultManager] enumeratorAtURL:cacheURL includingPropertiesForKeys:@[NSURLContentAccessDateKey] options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil]) { for (NSURL *fileURL in [[NSFileManager defaultManager] enumeratorAtURL:cacheURL includingPropertiesForKeys:@[NSURLContentAccessDateKey] options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil]) {
Expand All @@ -170,6 +179,7 @@ - (void)purgeDiskCacheOfImagesLastAccessedBefore:(NSDate *)date {
[[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil]; [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
} }
} }
[[UIApplication sharedApplication] endBackgroundTask:taskId];
}); });
} }


Expand Down

0 comments on commit fcdeb2c

Please sign in to comment.