Commit 403db190 authored by Chris Scott's avatar Chris Scott

Implement an NSTimer to ensure we end our background-task. Been getting app...

Implement an NSTimer to ensure we end our background-task.  Been getting app crashes referencing background job running beyond its permitted time
parent 2952b510
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
NSString *url; NSString *url;
NSString *syncCallbackId; NSString *syncCallbackId;
UIBackgroundTaskIdentifier bgTask; UIBackgroundTaskIdentifier bgTask;
NSTimer *backgroundTimer;
BOOL isMoving; BOOL isMoving;
NSNumber *maxBackgroundHours; NSNumber *maxBackgroundHours;
...@@ -125,13 +126,7 @@ ...@@ -125,13 +126,7 @@
-(void) finish:(CDVInvokedUrlCommand*)command -(void) finish:(CDVInvokedUrlCommand*)command
{ {
NSLog(@"CDVBackgroundGeoLocation finish"); NSLog(@"CDVBackgroundGeoLocation finish");
//_completionHandler(UIBackgroundFetchResultNewData); [self stopBackgroundTask];
// Finish the voodoo.
if (bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
} }
/** /**
...@@ -142,7 +137,7 @@ ...@@ -142,7 +137,7 @@
NSLog(@"CDVBackgroundGeoLocation suspend"); NSLog(@"CDVBackgroundGeoLocation suspend");
suspendedAt = [NSDate date]; suspendedAt = [NSDate date];
if (enabled) { if (enabled && !isMoving) {
[self setPace: NO]; [self setPace: NO];
} }
} }
...@@ -188,8 +183,16 @@ ...@@ -188,8 +183,16 @@
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask]; [app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}]; }];
// Set a timer to ensure our bgTask is murdered 1s before our remaining time expires.
backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:app.backgroundTimeRemaining-1
target:self
selector:@selector(onTimeExpired:)
userInfo:nil
repeats:NO];
// Fetch last recorded location // Fetch last recorded location
CLLocation *location = [locationCache lastObject]; CLLocation *location = [locationCache lastObject];
...@@ -213,6 +216,25 @@ ...@@ -213,6 +216,25 @@
// Inform javascript a background-fetch event has occurred. // Inform javascript a background-fetch event has occurred.
[self.commandDelegate sendPluginResult:result callbackId:syncCallbackId]; [self.commandDelegate sendPluginResult:result callbackId:syncCallbackId];
} }
- (void)onTimeExpired:(NSTimer *)timer
{
NSLog(@"- CDVBackgroundGeoLocation TIME EXPIRED");
[self stopBackgroundTask];
}
- (void) stopBackgroundTask
{
[backgroundTimer invalidate];
backgroundTimer = nil;
UIApplication *app = [UIApplication sharedApplication];
NSLog(@"CDVBackgroundGeoLocation stopBackgroundTask (remaining t: %f)", app.backgroundTimeRemaining);
if (bgTask != UIBackgroundTaskInvalid)
{
NSLog(@" -bgTask killed");
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}
/** /**
* Called when user exits their stationary radius (ie: they walked ~50m away from their last recorded location. * Called when user exits their stationary radius (ie: they walked ~50m away from their last recorded location.
* - turn on more aggressive location monitoring. * - turn on more aggressive location monitoring.
...@@ -249,7 +271,7 @@ ...@@ -249,7 +271,7 @@
- (void)setPace:(BOOL)value - (void)setPace:(BOOL)value
{ {
NSLog(@"- CDVBackgroundGeoLocation setPace %d", value); NSLog(@"- CDVBackgroundGeoLocation setPace %d", value);
isMoving = value;
if (myRegion != nil) { if (myRegion != nil) {
[locationManager stopMonitoringForRegion:myRegion]; [locationManager stopMonitoringForRegion:myRegion];
myRegion = nil; myRegion = nil;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment