Skip to content

Commit

Permalink
Allow system sleep when paused
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgio Calderolla committed Sep 10, 2014
1 parent 21f1c9c commit ccbc962
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions CTCScheduler.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ - (id)init {
if (!self) {
return nil;
}


self.activityToken = nil;
self.polling = YES;
self.checking = NO;

Expand All @@ -61,7 +62,7 @@ - (id)init {
// Check now as well
[self fireTimerNow];

[self preventAppNap];
[self updateAppNapStatus];

return self;
}
Expand All @@ -71,10 +72,22 @@ - (void)preventAppNap {
if ([NSProcessInfo.processInfo respondsToSelector:@selector(beginActivityWithOptions:reason:)]) {
self.activityToken = [NSProcessInfo.processInfo
beginActivityWithOptions:NSActivityIdleSystemSleepDisabled|NSActivitySuddenTerminationDisabled
reason:@"Background checking is the whole point of the app"];
reason:@"Actively polling the feed"];
}
}

- (void)allowAppNap {
// Make sure we can keep running in the background if the system supports App Nap
if ([NSProcessInfo.processInfo respondsToSelector:@selector(beginActivityWithOptions:reason:)] && self.activityToken) {
[NSProcessInfo.processInfo endActivity:self.activityToken];
self.activityToken = nil;
}
}

- (void)updateAppNapStatus {
self.polling ? [self preventAppNap] : [self allowAppNap];
}

- (void)setChecking:(BOOL)checking {
_checking = checking;
[self reportStatus];
Expand All @@ -83,6 +96,8 @@ - (void)setChecking:(BOOL)checking {
- (void)setPolling:(BOOL)polling {
_polling = polling;

[self updateAppNapStatus];

[self reportStatus];
}

Expand Down

0 comments on commit ccbc962

Please sign in to comment.