Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove necessity of declaring PIN_APP_EXTENSIONS macro #72

Merged
merged 4 commits into from
Mar 5, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions PINCache/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
static NSString * const PINDiskCacheSharedName = @"PINDiskCacheShared";

@interface PINBackgroundTask : NSObject
#if !defined(PIN_APP_EXTENSIONS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH
@property (atomic, assign) UIBackgroundTaskIdentifier taskID;
#endif
+ (instancetype)start;
Expand Down Expand Up @@ -1107,10 +1107,16 @@ - (void)unlock
@end

@implementation PINBackgroundTask

+ (BOOL)isAppExtension
{
return [[[NSBundle mainBundle] executablePath] containsString:@".appex/"];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be dispatch_once'd?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a unit test for this? I think this is doable if you separated this into 2 methods, once of which just took a string and return a BOOL, and isAppExtension could call that one with [[NSBundle mainBundle] executablePath]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this will be dispatched_once. I will look into how we could integrate a unit test for it.

}

- (instancetype)init
{
if (self = [super init]) {
#if !defined(PIN_APP_EXTENSIONS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH
_taskID = UIBackgroundTaskInvalid;
#endif
}
Expand All @@ -1120,22 +1126,34 @@ - (instancetype)init
+ (instancetype)start
{
PINBackgroundTask *task = [[self alloc] init];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@garrettmoon You think we should just return nil here and not allocate any PINBackgroundTask if isAppExtension == YES?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a good call to me.

#if !defined(PIN_APP_EXTENSIONS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH
task.taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH
if ([self.class isAppExtension]) {
return task;
}

UIApplication *sharedApplication = [UIApplication performSelector:@selector(sharedApplication)];
task.taskID = [sharedApplication beginBackgroundTaskWithExpirationHandler:^{
UIBackgroundTaskIdentifier taskID = task.taskID;
task.taskID = UIBackgroundTaskInvalid;
[[UIApplication sharedApplication] endBackgroundTask:taskID];
[sharedApplication endBackgroundTask:taskID];
}];
#endif
return task;
}

- (void)end
{
#if !defined(PIN_APP_EXTENSIONS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0 && !TARGET_OS_WATCH
if ([self.class isAppExtension]) {
return;
}

UIBackgroundTaskIdentifier taskID = self.taskID;
self.taskID = UIBackgroundTaskInvalid;
[[UIApplication sharedApplication] endBackgroundTask:taskID];

UIApplication *sharedApplication = [UIApplication performSelector:@selector(sharedApplication)];
[sharedApplication endBackgroundTask:taskID];
#endif
}

Expand Down