Lifecycle and Usage of CLServiceSession after the app is terminated

Hi,

I am creating a Driving Behaviour Monitoring app in which I range beacons and I require location updates in foreground, background and in terminated state all the time.

I am using CLServiceSession with "Always Authorisation" to get location events. I create CLServiceSession object in the foreground and start monitoring driving and then re-create it when the app is relaunched after termination. Doing this works fine.

But sometimes when app is terminated and is not opened again, the app runs on its own even when the device is stationary ( I can see the app is using Location in the Control Centre) and after that Location updates are not received and I am not able to track the driving behaviour. I tried to add diagnostics to know the cause and found "Insufficiently In Use" and then "Service Session Required" in the diagnostics.

It would be of great help if the proper usage of CLServiceSession is provided.

Important Question: When does the CLServiceSession gets invalidated or destroyed that was created when the app was in foreground ? What happens to the CLServiceSession which was created in the foreground if the app is not opened for long duration, let's say a day or two?

The CLServiceSession does not get invalidated when the app is not running, due to time passing.

It gets invalidated the next time the app is launched, either due to a location update, or some other reason in the background. Or even when the user launches it (but that case does not pose a problem as the app would be sufficiently in use)

When the app is re-launched, the old CLServiceSession gets invalidated and will be destroyed, and the system expects you to immediately recreate a new CLServiceSession within a few seconds of launch.

If not, you will be considered no longer wanting to continue location updates, and you will end up in the background without a CLServiceSession.

So, you are seeing this problem, not because the CLServiceSession is invalidated due to the passing of time, but because you are not creating a new one in time - which could be due to the passing of time, if your launch logic is doing some extra work due to the passing of time.

If you make sure the CLServiceSession is created as the first thing at launch, you should not be running into this issue.


Argun Tekant /  DTS Engineer / Core Technologies

Lifecycle and Usage of CLServiceSession after the app is terminated
 
 
Q