Skip to content

Commit

Permalink
Merge pull request #14 from pinterest/fixBackgroundTaskID
Browse files Browse the repository at this point in the history
Invalid task ids used for expiration handlers. #13
  • Loading branch information
garrettmoon committed Jul 30, 2015
2 parents 01a0efe + 446cc4e commit d4288aa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
81 changes: 55 additions & 26 deletions PINCache/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@
[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
__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];
#else
#define PINCacheStartBackgroundTask()
#define PINCacheEndBackgroundTask()
#endif

NSString * const PINDiskCachePrefix = @"com.pinterest.PINDiskCache";
NSString * const PINDiskCacheSharedName = @"PINDiskCacheShared";

@interface PINBackgroundTask : NSObject
@property (atomic, assign) UIBackgroundTaskIdentifier taskID;
+ (instancetype)start;
- (void)end;
@end

@interface PINDiskCache ()

@property (assign) NSUInteger byteCount;
Expand Down Expand Up @@ -219,7 +215,7 @@ +(BOOL)moveItemAtURLToTrash:(NSURL *)itemURL

+ (void)emptyTrash
{
PINCacheStartBackgroundTask();
PINBackgroundTask *task = [PINBackgroundTask start];

dispatch_async([self sharedTrashQueue], ^{
NSError *error = nil;
Expand All @@ -235,7 +231,7 @@ + (void)emptyTrash
PINDiskCacheError(error);
}

PINCacheEndBackgroundTask();
[task end];
});
}

Expand Down Expand Up @@ -656,7 +652,7 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key fileURL:(NSURL **
if (!key || !object)
return;

PINCacheStartBackgroundTask();
PINBackgroundTask *task = [PINBackgroundTask start];

NSURL *fileURL = nil;

Expand Down Expand Up @@ -695,7 +691,7 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key fileURL:(NSURL **
*outFileURL = fileURL;
}

PINCacheEndBackgroundTask();
[task end];
}

- (void)removeObjectForKey:(NSString *)key
Expand All @@ -708,7 +704,7 @@ - (void)removeObjectForKey:(NSString *)key fileURL:(NSURL **)outFileURL
if (!key)
return;

PINCacheStartBackgroundTask();
PINBackgroundTask *task = [PINBackgroundTask start];

NSURL *fileURL = nil;

Expand All @@ -717,7 +713,7 @@ - (void)removeObjectForKey:(NSString *)key fileURL:(NSURL **)outFileURL
[self removeFileAndExecuteBlocksForKey:key];
[self unlock];

PINCacheEndBackgroundTask();
[task end];

if (outFileURL) {
*outFileURL = fileURL;
Expand All @@ -731,13 +727,13 @@ - (void)trimToSize:(NSUInteger)trimByteCount
return;
}

PINCacheStartBackgroundTask();
PINBackgroundTask *task = [PINBackgroundTask start];

[self lock];
[self trimDiskToSize:trimByteCount];
[self unlock];

PINCacheEndBackgroundTask();
[task end];
}

- (void)trimToDate:(NSDate *)trimDate
Expand All @@ -750,13 +746,13 @@ - (void)trimToDate:(NSDate *)trimDate
return;
}

PINCacheStartBackgroundTask();
PINBackgroundTask *task = [PINBackgroundTask start];

[self lock];
[self trimDiskToDate:trimDate];
[self unlock];

PINCacheEndBackgroundTask();
[task end];
}

- (void)trimToSizeByDate:(NSUInteger)trimByteCount
Expand All @@ -766,18 +762,18 @@ - (void)trimToSizeByDate:(NSUInteger)trimByteCount
return;
}

PINCacheStartBackgroundTask();
PINBackgroundTask *task = [PINBackgroundTask start];

[self lock];
[self trimDiskToSizeByDate:trimByteCount];
[self unlock];

PINCacheEndBackgroundTask();
[task end];
}

- (void)removeAllObjects
{
PINCacheStartBackgroundTask();
PINBackgroundTask *task = [PINBackgroundTask start];

[self lock];
if (self->_willRemoveAllObjectsBlock)
Expand All @@ -796,15 +792,15 @@ - (void)removeAllObjects
self->_didRemoveAllObjectsBlock(self);
[self unlock];

PINCacheEndBackgroundTask();
[task end];
}

- (void)enumerateObjectsWithBlock:(PINDiskCacheObjectBlock)block
{
if (!block)
return;

PINCacheStartBackgroundTask();
PINBackgroundTask *task = [PINBackgroundTask start];

[self lock];
NSArray *keysSortedByDate = [self->_dates keysSortedByValueUsingSelector:@selector(compare:)];
Expand All @@ -815,7 +811,7 @@ - (void)enumerateObjectsWithBlock:(PINDiskCacheObjectBlock)block
}
[self unlock];

PINCacheEndBackgroundTask();
[task end];
}

#pragma mark - Public Thread Safe Accessors -
Expand Down Expand Up @@ -1043,3 +1039,36 @@ - (void)unlock
}

@end

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

+ (instancetype)start
{
PINBackgroundTask *task = [[self alloc] init];
#if !defined(PIN_APP_EXTENSIONS)
task.taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
UIBackgroundTaskIdentifier taskID = task.taskID;
task.taskID = UIBackgroundTaskInvalid;
[[UIApplication sharedApplication] endBackgroundTask:taskID];
}];
#endif
return task;
}

- (void)end
{
#if !defined(PIN_APP_EXTENSIONS)
UIBackgroundTaskIdentifier taskID = self.taskID;
self.taskID = UIBackgroundTaskInvalid;
[[UIApplication sharedApplication] endBackgroundTask:taskID];
#endif
}

@end
6 changes: 1 addition & 5 deletions tests/PINCache.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
D07F1EC2171AFB7A001DBA02 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D07F1EC1171AFB7A001DBA02 /* main.m */; };
D07F1EC6171AFB7A001DBA02 /* PINAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D07F1EC5171AFB7A001DBA02 /* PINAppDelegate.m */; };
D07F1ECC171AFB7A001DBA02 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = D07F1ECB171AFB7A001DBA02 /* [email protected] */; };
D07F1ED4171AFB7A001DBA02 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D07F1ED3171AFB7A001DBA02 /* SenTestingKit.framework */; };
D07F1ED5171AFB7A001DBA02 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D07F1EB5171AFB7A001DBA02 /* UIKit.framework */; };
D07F1ED6171AFB7A001DBA02 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D07F1EB7171AFB7A001DBA02 /* Foundation.framework */; };
D07F1EE1171AFB7A001DBA02 /* PINCacheTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D07F1EE0171AFB7A001DBA02 /* PINCacheTests.m */; };
Expand Down Expand Up @@ -79,7 +78,6 @@
D07F1EC5171AFB7A001DBA02 /* PINAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PINAppDelegate.m; sourceTree = "<group>"; };
D07F1ECB171AFB7A001DBA02 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
D07F1ED2171AFB7A001DBA02 /* PINCacheTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PINCacheTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D07F1ED3171AFB7A001DBA02 /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
D07F1EDB171AFB7A001DBA02 /* PINCacheTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "PINCacheTests-Info.plist"; sourceTree = "<group>"; };
D07F1EDF171AFB7A001DBA02 /* PINCacheTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PINCacheTests.h; sourceTree = "<group>"; };
D07F1EE0171AFB7A001DBA02 /* PINCacheTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = PINCacheTests.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
Expand Down Expand Up @@ -123,7 +121,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D07F1ED4171AFB7A001DBA02 /* SenTestingKit.framework in Frameworks */,
D07F1ED5171AFB7A001DBA02 /* UIKit.framework in Frameworks */,
D07F1ED6171AFB7A001DBA02 /* Foundation.framework in Frameworks */,
);
Expand Down Expand Up @@ -195,7 +192,6 @@
D07F1EB5171AFB7A001DBA02 /* UIKit.framework */,
D07F1EB7171AFB7A001DBA02 /* Foundation.framework */,
D07F1EB9171AFB7A001DBA02 /* CoreGraphics.framework */,
D07F1ED3171AFB7A001DBA02 /* SenTestingKit.framework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down Expand Up @@ -355,7 +351,7 @@
isa = PBXProject;
attributes = {
CLASSPREFIX = TM;
LastTestingUpgradeCheck = 0620;
LastTestingUpgradeCheck = 0640;
LastUpgradeCheck = 0500;
ORGANIZATIONNAME = Tumblr;
TargetAttributes = {
Expand Down

0 comments on commit d4288aa

Please sign in to comment.