Commit 1253c4b9 authored by Chris Scott's avatar Chris Scott

Improved thread-mgmt to prevent crashes

parent 52526e25
// //
// CDVBackgroundGeoLocation.hs // CDVBackgroundGeoLocation.h
// //
// Created by Chris Scott <chris@transistorsoft.com> // Created by Chris Scott <chris@transistorsoft.com>
// //
......
...@@ -170,7 +170,18 @@ ...@@ -170,7 +170,18 @@
// old, too close to the previous one, too inaccurate and so forth according to your own // old, too close to the previous one, too inaccurate and so forth according to your own
// application design. // application design.
[locationCache addObjectsFromArray:locations]; [locationCache addObjectsFromArray:locations];
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self stopBackgroundTask];
});
}];
[self.commandDelegate runInBackground:^{
[self sync]; [self sync];
}];
} }
/** /**
* We are running in the background if this is being executed. * We are running in the background if this is being executed.
...@@ -179,15 +190,6 @@ ...@@ -179,15 +190,6 @@
*/ */
-(void) sync -(void) sync
{ {
NSLog(@"- CDVBackgroundGeoLocation sync");
UIApplication *app = [UIApplication sharedApplication];
// Inform javascript a background-fetch event has occurred.
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[self stopBackgroundTask];
}];
// Fetch last recorded location // Fetch last recorded location
CLLocation *location = [locationCache lastObject]; CLLocation *location = [locationCache lastObject];
...@@ -202,14 +204,6 @@ ...@@ -202,14 +204,6 @@
[returnInfo setObject:[NSNumber numberWithDouble:location.coordinate.latitude] forKey:@"latitude"]; [returnInfo setObject:[NSNumber numberWithDouble:location.coordinate.latitude] forKey:@"latitude"];
[returnInfo setObject:[NSNumber numberWithDouble:location.coordinate.longitude] forKey:@"longitude"]; [returnInfo setObject:[NSNumber numberWithDouble:location.coordinate.longitude] forKey:@"longitude"];
// 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];
[self.commandDelegate runInBackground:^{
// Build a resultset for javascript callback. // Build a resultset for javascript callback.
CDVPluginResult* result = nil; CDVPluginResult* result = nil;
...@@ -217,23 +211,14 @@ ...@@ -217,23 +211,14 @@
[result setKeepCallbackAsBool:YES]; [result setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:result callbackId:syncCallbackId]; [self.commandDelegate sendPluginResult:result callbackId:syncCallbackId];
}];
}
- (void)onTimeExpired:(NSTimer *)timer
{
NSLog(@"- CDVBackgroundGeoLocation TIME EXPIRED");
[self stopBackgroundTask];
} }
- (void) stopBackgroundTask - (void) stopBackgroundTask
{ {
[backgroundTimer invalidate];
backgroundTimer = nil;
UIApplication *app = [UIApplication sharedApplication]; UIApplication *app = [UIApplication sharedApplication];
NSLog(@"- CDVBackgroundGeoLocation stopBackgroundTask (remaining t: %f)", app.backgroundTimeRemaining); NSLog(@"- CDVBackgroundGeoLocation stopBackgroundTask (remaining t: %f)", app.backgroundTimeRemaining);
if (bgTask != UIBackgroundTaskInvalid) if (bgTask != UIBackgroundTaskInvalid)
{ {
[[UIApplication sharedApplication] endBackgroundTask:bgTask]; [app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid; bgTask = UIBackgroundTaskInvalid;
} }
} }
......
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