Skip to content

Commit

Permalink
Invalid task ids used for expiration handlers. #13
Browse files Browse the repository at this point in the history
This should fix two issues:
- Because taskID was being captured before being set, the expiration handler had an invalid taskID.
- If the expiration handler was called, and then PINCacheEndBackgroundTask was called, it would try to end a task that had already ended.
  • Loading branch information
garrettmoon committed Jul 28, 2015
1 parent 01a0efe commit cd3ff7b
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions PINCache/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
__LINE__, [error localizedDescription]); }

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !defined(PIN_APP_EXTENSIONS)
#define PINCacheStartBackgroundTask() UIBackgroundTaskIdentifier taskID = UIBackgroundTaskInvalid; \
taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ \
[[UIApplication sharedApplication] endBackgroundTask:taskID]; }];
#define PINCacheEndBackgroundTask() [[UIApplication sharedApplication] endBackgroundTask:taskID];
#define PINCacheStartBackgroundTask() \
PINBackgroundTask *task = [[PINBackgroundTask alloc] init]; \
task.taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ \
UIBackgroundTaskIdentifier taskID = task.taskID; \
task.taskID = UIBackgroundTaskInvalid; \
[[UIApplication sharedApplication] endBackgroundTask:taskID]; \
}];
#define PINCacheEndBackgroundTask() \
[[UIApplication sharedApplication] endBackgroundTask:task.taskID];
#else
#define PINCacheStartBackgroundTask()
#define PINCacheEndBackgroundTask()
Expand All @@ -25,6 +30,20 @@
NSString * const PINDiskCachePrefix = @"com.pinterest.PINDiskCache";
NSString * const PINDiskCacheSharedName = @"PINDiskCacheShared";

@interface PINBackgroundTask : NSObject
@property (atomic, assign) UIBackgroundTaskIdentifier taskID;
@end

@implementation PINBackgroundTask
- (instancetype)init
{
if (self = [super init]) {
_taskID = UIBackgroundTaskInvalid;
}
return self;
}
@end

@interface PINDiskCache ()

@property (assign) NSUInteger byteCount;
Expand Down Expand Up @@ -220,7 +239,6 @@ +(BOOL)moveItemAtURLToTrash:(NSURL *)itemURL
+ (void)emptyTrash
{
PINCacheStartBackgroundTask();

dispatch_async([self sharedTrashQueue], ^{
NSError *error = nil;
NSArray *trashedItems = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[self sharedTrashURL]
Expand Down

0 comments on commit cd3ff7b

Please sign in to comment.