diff --git a/PINCache/PINDiskCache.m b/PINCache/PINDiskCache.m index 3f11e727..4da336b1 100644 --- a/PINCache/PINDiskCache.m +++ b/PINCache/PINDiskCache.m @@ -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; @@ -219,7 +215,7 @@ +(BOOL)moveItemAtURLToTrash:(NSURL *)itemURL + (void)emptyTrash { - PINCacheStartBackgroundTask(); + PINBackgroundTask *task = [PINBackgroundTask start]; dispatch_async([self sharedTrashQueue], ^{ NSError *error = nil; @@ -235,7 +231,7 @@ + (void)emptyTrash PINDiskCacheError(error); } - PINCacheEndBackgroundTask(); + [task end]; }); } @@ -656,7 +652,7 @@ - (void)setObject:(id )object forKey:(NSString *)key fileURL:(NSURL ** if (!key || !object) return; - PINCacheStartBackgroundTask(); + PINBackgroundTask *task = [PINBackgroundTask start]; NSURL *fileURL = nil; @@ -695,7 +691,7 @@ - (void)setObject:(id )object forKey:(NSString *)key fileURL:(NSURL ** *outFileURL = fileURL; } - PINCacheEndBackgroundTask(); + [task end]; } - (void)removeObjectForKey:(NSString *)key @@ -708,7 +704,7 @@ - (void)removeObjectForKey:(NSString *)key fileURL:(NSURL **)outFileURL if (!key) return; - PINCacheStartBackgroundTask(); + PINBackgroundTask *task = [PINBackgroundTask start]; NSURL *fileURL = nil; @@ -717,7 +713,7 @@ - (void)removeObjectForKey:(NSString *)key fileURL:(NSURL **)outFileURL [self removeFileAndExecuteBlocksForKey:key]; [self unlock]; - PINCacheEndBackgroundTask(); + [task end]; if (outFileURL) { *outFileURL = fileURL; @@ -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 @@ -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 @@ -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) @@ -796,7 +792,7 @@ - (void)removeAllObjects self->_didRemoveAllObjectsBlock(self); [self unlock]; - PINCacheEndBackgroundTask(); + [task end]; } - (void)enumerateObjectsWithBlock:(PINDiskCacheObjectBlock)block @@ -804,7 +800,7 @@ - (void)enumerateObjectsWithBlock:(PINDiskCacheObjectBlock)block if (!block) return; - PINCacheStartBackgroundTask(); + PINBackgroundTask *task = [PINBackgroundTask start]; [self lock]; NSArray *keysSortedByDate = [self->_dates keysSortedByValueUsingSelector:@selector(compare:)]; @@ -815,7 +811,7 @@ - (void)enumerateObjectsWithBlock:(PINDiskCacheObjectBlock)block } [self unlock]; - PINCacheEndBackgroundTask(); + [task end]; } #pragma mark - Public Thread Safe Accessors - @@ -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 diff --git a/tests/PINCache.xcodeproj/project.pbxproj b/tests/PINCache.xcodeproj/project.pbxproj index 294a24c3..fb5bf645 100644 --- a/tests/PINCache.xcodeproj/project.pbxproj +++ b/tests/PINCache.xcodeproj/project.pbxproj @@ -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 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = D07F1ECB171AFB7A001DBA02 /* Default-568h@2x.png */; }; - 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 */; }; @@ -79,7 +78,6 @@ D07F1EC5171AFB7A001DBA02 /* PINAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PINAppDelegate.m; sourceTree = ""; }; D07F1ECB171AFB7A001DBA02 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; 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 = ""; }; D07F1EDF171AFB7A001DBA02 /* PINCacheTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PINCacheTests.h; sourceTree = ""; }; D07F1EE0171AFB7A001DBA02 /* PINCacheTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = PINCacheTests.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; @@ -123,7 +121,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - D07F1ED4171AFB7A001DBA02 /* SenTestingKit.framework in Frameworks */, D07F1ED5171AFB7A001DBA02 /* UIKit.framework in Frameworks */, D07F1ED6171AFB7A001DBA02 /* Foundation.framework in Frameworks */, ); @@ -195,7 +192,6 @@ D07F1EB5171AFB7A001DBA02 /* UIKit.framework */, D07F1EB7171AFB7A001DBA02 /* Foundation.framework */, D07F1EB9171AFB7A001DBA02 /* CoreGraphics.framework */, - D07F1ED3171AFB7A001DBA02 /* SenTestingKit.framework */, ); name = Frameworks; sourceTree = ""; @@ -355,7 +351,7 @@ isa = PBXProject; attributes = { CLASSPREFIX = TM; - LastTestingUpgradeCheck = 0620; + LastTestingUpgradeCheck = 0640; LastUpgradeCheck = 0500; ORGANIZATIONNAME = Tumblr; TargetAttributes = {