diff options
-rw-r--r-- | iphone/Maps/Classes/LocalNotificationManager.mm | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/iphone/Maps/Classes/LocalNotificationManager.mm b/iphone/Maps/Classes/LocalNotificationManager.mm index 5eefef9dd1..ddf9ec4e66 100644 --- a/iphone/Maps/Classes/LocalNotificationManager.mm +++ b/iphone/Maps/Classes/LocalNotificationManager.mm @@ -239,12 +239,18 @@ typedef void (^CompletionHandler)(UIBackgroundFetchResult); - (void)showDownloadMapNotificationIfNeeded:(void (^)(UIBackgroundFetchResult))completionHandler { - self.downloadMapCompletionHandler = completionHandler; - self.timer = [NSTimer scheduledTimerWithTimeInterval:25 target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO]; - if ([CLLocationManager locationServicesEnabled]) + NSTimeInterval const completionTimeIndent = 2.0; + NSTimeInterval const backgroundTimeRemaining = UIApplication.sharedApplication.backgroundTimeRemaining - completionTimeIndent; + if ([CLLocationManager locationServicesEnabled] && backgroundTimeRemaining > 0.0) + { + self.downloadMapCompletionHandler = completionHandler; + self.timer = [NSTimer scheduledTimerWithTimeInterval:backgroundTimeRemaining target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO]; [self.locationManager startUpdatingLocation]; + } else + { completionHandler(UIBackgroundFetchResultFailed); + } } - (void)markNotificationShowingForIndex:(TIndex)index @@ -271,7 +277,7 @@ typedef void (^CompletionHandler)(UIBackgroundFetchResult); { // Location still was not received but it's time to finish up so system will not kill us. [self.locationManager stopUpdatingLocation]; - self.downloadMapCompletionHandler(UIBackgroundFetchResultFailed); + [self performCompletionHandler:UIBackgroundFetchResultFailed]; } - (void)downloadCountryWithIndex:(TIndex)index @@ -301,6 +307,14 @@ typedef void (^CompletionHandler)(UIBackgroundFetchResult); return TIndex(); } +- (void)performCompletionHandler:(UIBackgroundFetchResult)result +{ + if (!self.downloadMapCompletionHandler) + return; + self.downloadMapCompletionHandler(result); + self.downloadMapCompletionHandler = nil; +} + #pragma mark - Location Manager - (CLLocationManager *)locationManager @@ -352,7 +366,7 @@ typedef void (^CompletionHandler)(UIBackgroundFetchResult); } } [[Statistics instance] logEvent:flurryEventName withParameters:@{@"WiFi" : @(onWiFi)}]; - self.downloadMapCompletionHandler(result); + [self performCompletionHandler:result]; } @end |