1

I'm writing a Cordova plugin for background tracking in iOS, my understanding is the following:

  • I use startUpdatingLocation to start the tracking
  • this will send locations whether my app is in the foreground or in the background
  • if I receive a location in the background and take too much time to process it, iOS will kill the app
  • this is why I also use startMonitoringSignificantLocationChanges, so in case my app is killed iOS will reopen it with a new location in didFinishLaunchingWithOptions

is that correct?

I also have two other questions:

Monitoring significant location changes

I call startMonitoringSignificantLocationChanges to have my app relaunched if it is killed. I have read that in this case, the latest location is given in the options passed to the method, but that we have to call again both startUpdatingLocation and startMonitoringSignificantLocationChanges to keep the geolocation coming, is this correct ?

Also, if we have to call startUpdatingLocation, and (I assume) this generates a new call to the location handler with the latest location, is there a point in using the location given as parameter directly in the didFinishLaunchingWithOptions method rather than in the location handler?

It seems this would result in two parts of the code with the same processing task so this is redundant (I assume the location processing is also done in the location handler since this was the purpose of the app before it got killed -- the only case I see that might need processing in didFinishLaunchingWithOptions without restarting startUpdatingLocation is geofencing apps that only need to be awaken near a particular location without necessarily tracking the user, is this why the option is given?)

Now the question: is there a way when writing a Cordova plugin to add directly a handler to the didFinishLaunchingWithOptions method in order to call back startUpdatingLocation and startMonitoringSignificantChanges again? or is the only way to listen to the corresponding notification from the plugin code?

Javascript events when processing geolocations

The plugin calls a javascript event when it receive a location in the foreground or background. However, this javascript code might run some tasks like an animation with a timer, etc. I'd like to understand better the Cordova execution to see whether this would result in a delay that causes the app to be killed if for instance receiving a geolocation starts a javascript timer that runs an animation for too long, does this keep the application running and triggers the iOS watchdog, or is it run in a BackgroundTask thread?

And for instance if I run an AngularJS app that reacts to a new location, what happens since AngularJS has it's own run loop, will the application always be killed because AngularJS is constantly watching the variables for change etc & I have to tell AngularJS to stop its run loop when the app is in the background?

In particular, what happens if I want to asynchronously send an HTTP request to a server? Will Cordova run the js in the background or do I have to tell Cordova to wait until a certain event happens, eg the js code does the request and tells Cordova it has finished so Cordova can finish the background task?

oulipo
  • 335
  • 6
  • 18
  • "if I receive a location in the background and take too much time to process it, iOS will kill the app". Where have you got that from? – Gruntcakes Apr 28 '14 at 22:45
  • Isn't there something like if the app takes 10m of processing in the background it is automatically killed? https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW20 – oulipo Apr 28 '14 at 22:47
  • Perhaps you are thinking about beginBackgroundTaskWithExpirationHandler: which permits an app to execute in the background for 10 minutes (well actually now its about 3 in iOS 7). But that is applicable for apps that don't have a background mode, but your app has the location background mode. And then it wouldn't "kill" the app. Killing and stopping executing after 10 minutes are most certainly not the same thing. So answer your question, no its not correct. – Gruntcakes Apr 28 '14 at 22:51
  • So if I understand correctly, when I add the `location` background mode, the iOS watchdog does not apply and iOS will neither suspend nor kill the app (except if the memory is full, in which case I guess I still have to use `monitorSignificantLocationChanges` if I want my app to be reopened?) – oulipo Apr 28 '14 at 22:55
  • There's lots of discussion here http://stackoverflow.com/questions/12239967/will-ios-wake-up-the-terminated-app-if-its-registered-with-location-for-uibackg – Gruntcakes Apr 28 '14 at 23:01
  • Thanks, BTW the answers to the other questions interest me if you know them – oulipo Apr 28 '14 at 23:07
  • This is iOS not Cordova -- if your app is relaunched after being terminated because a new significant location has been found, this location is given in parameter to `didFinishLaunchingWithOptions` which is a `UIApplication` method – oulipo Apr 28 '14 at 23:19
  • From https://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html: "If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available." – oulipo Apr 28 '14 at 23:22
  • The location info returned from the two methods is not necessarily redundant because I would bet that the accuracy from startMonitoringSignificantLocationChanges is very low, however the accuracy from startUpdatingLocation: can go very very high (if you request it). So you can use the first to ensure the app gets launched but the second to get fine accuracy. IMO you need to do a very lot of real-use observation when using the location APIs, I reccommend putting in persistent logging (i.e. cocoa lumberjack) and just observing the behavior of the device. – Gruntcakes Apr 28 '14 at 23:35
  • Regarding the background execution time, here is what I found on https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW47 "Do minimal work while running in the background. The execution time given to background apps is more constrained than the amount of time given to the foreground app. Apps that spend too much time executing in the background can be throttled back by the system or terminated." – oulipo Apr 29 '14 at 09:54

1 Answers1

0

I created a Cordova plugin cordova-background-geolocation that handles this.

Yes, you need to be careful to execute your javascript callback in a background-thread and have the javascript signal back to the native side when it has finished on its end (eg. after an async. request to server) so that the native side can kill the thread.

Christocracy
  • 103
  • 1
  • 7