From ccbc9627fc25a5f85494c65b5d511c7e5c67394a Mon Sep 17 00:00:00 2001 From: Giorgio Calderolla Date: Wed, 10 Sep 2014 22:18:15 +0200 Subject: [PATCH] Allow system sleep when paused --- CTCScheduler.m | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/CTCScheduler.m b/CTCScheduler.m index cea6461..5ce2fd2 100644 --- a/CTCScheduler.m +++ b/CTCScheduler.m @@ -37,7 +37,8 @@ - (id)init { if (!self) { return nil; } - + + self.activityToken = nil; self.polling = YES; self.checking = NO; @@ -61,7 +62,7 @@ - (id)init { // Check now as well [self fireTimerNow]; - [self preventAppNap]; + [self updateAppNapStatus]; return self; } @@ -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]; @@ -83,6 +96,8 @@ - (void)setChecking:(BOOL)checking { - (void)setPolling:(BOOL)polling { _polling = polling; + [self updateAppNapStatus]; + [self reportStatus]; }