Hello, I am using CLLocationManager to monitor multiple CLBeaconRegion instances (up to 20). When the app is terminated by the system (not force-quit) and a region enter event occurs, the app is relaunched in the background. I have two questions: What is the expected execution time window after relaunch before the app is suspended again? Is it supported to start short CoreBluetooth operations (e.g., scanning or connecting briefly) within this window? I understand that force-quitting the app disables background relaunch, so this question applies only to system-terminated apps.
Background execution window after CLBeaconRegion wake from terminated state
Hello, I am using CLLocationManager to monitor multiple CLBeaconRegion instances (up to 20). When the app is terminated by the system (not force-quit) and a region enter event occurs, the app is relaunched in the background. I have two questions: What is the expected execution time window after relaunch before the app is suspended again?
This is a trickier question than it seems, as there is a pretty big difference between what our documentation says and real-world behavior. Splitting those questions up:
Documented Behavior:
With a few exceptions, our documentation doesn't actually say how long your process will stay awake after it's been woken, so strictly speaking, the system COULD suspend your process immediately after it returns from whatever callback woke it. That leads to...
Actual Behavior:
...however, the actual behavior here varies WIDELY, typically with an upper boundary of ~30s and a lower bound of "immediately after return". Generally speaking, the difference here matches up with when the API was introduced, with older APIs having much longer time windows than newer APIs. However...
Solution:
...the correct answer here is that you should NOT be relying on the system to keep your process awake. If you want to be awake, use UIApplication.beginBackgroundTask(withName:...) to keep yourself awake. That will give you ~30s of runtime, which you can use for whatever you need to do. Note that this recommendation applies to basically ALL cases— anytime you're asking "how long will the system keep me awake", the proper approach is to assume that it won't and use beginBackgroundTask to keep yourself awake instead.
That leads to here:
Is it supported to start short CoreBluetooth operations (e.g., scanning or connecting briefly) within this window?
In general, the reason an app is awake in the background doesn't really change orrestrict what that app can do in the background. So, yes, this is possible. Note that CoreBluetooth also has its own background category to allow extended Bluetooth interactions from the background, which could then be used for longer exchanges.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware