Permalink
Browse files

makes potentially long-running purge methods run as background tasks

  • Loading branch information...
1 parent 9e9d6fe commit fcdeb2c0e8ebdf2199cc32c67871f5c0c5cbb020 @artgillespie committed Aug 22, 2013
Showing with 10 additions and 0 deletions.
  1. +10 −0 OGImage/OGImageCache.m
View
@@ -122,10 +122,15 @@ - (void)setImage:(__OGImage *)image forKey:(NSString *)key {
- (void)purgeCache:(BOOL)wait {
[_memoryCache removeAllObjects];
+ UIBackgroundTaskIdentifier taskId = UIBackgroundTaskInvalid;
+ taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
+ [[UIApplication sharedApplication] endBackgroundTask:taskId];
+ }];
void (^purgeFilesBlock)(void) = ^{
for (NSURL *url in [[NSFileManager defaultManager] enumeratorAtURL:OGImageCacheURL() includingPropertiesForKeys:nil options:0 errorHandler:nil]) {
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
}
+ [[UIApplication sharedApplication] endBackgroundTask:taskId];
};
if (YES == wait) {
dispatch_sync(_cacheFileTasksQueue, purgeFilesBlock);
@@ -159,6 +164,10 @@ - (void)purgeMemoryCacheForKey:(NSString *)key andWait:(BOOL)wait {
}
- (void)purgeDiskCacheOfImagesLastAccessedBefore:(NSDate *)date {
+ UIBackgroundTaskIdentifier taskId = UIBackgroundTaskInvalid;
+ taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
+ [[UIApplication sharedApplication] endBackgroundTask:taskId];
+ }];
dispatch_async(_cacheFileTasksQueue, ^{
NSURL *cacheURL = OGImageCacheURL();
for (NSURL *fileURL in [[NSFileManager defaultManager] enumeratorAtURL:cacheURL includingPropertiesForKeys:@[NSURLContentAccessDateKey] options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil]) {
@@ -170,6 +179,7 @@ - (void)purgeDiskCacheOfImagesLastAccessedBefore:(NSDate *)date {
[[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
}
}
+ [[UIApplication sharedApplication] endBackgroundTask:taskId];
});
}

0 comments on commit fcdeb2c

Please sign in to comment.