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?

Answered by Engineer in 854838022

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

Accepted Answer

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

Hi,

Thanks for your help!

I was able to run the app without any issue but after upgrading Xcode to v26.x.x, I am having similar issue again.

I’m encountering an issue with background location tracking stopping after the app remains in the background/killed mode for around 1–2 days.

After some time in the background, the location events stop arriving entirely. While diagnosing the issue, I noticed logs/messages related to “service session required”.

I would like clarification regarding CLServiceSession lifecycle management.

Currently, I create the session locally inside a function like this:

let session = CLServiceSession(
    authorization: .always,
    fullAccuracyPurposeKey: "Trip"
)

I’m wondering whether the session being scoped locally could cause the service session to be deallocated or invalidated, eventually stopping background location delivery.

Would it be more appropriate to maintain a strong reference at the class level, for example:

private var serviceSession: CLServiceSession?

and then initialize it as:

serviceSession = CLServiceSession(
    authorization: .always,
    fullAccuracyPurposeKey: "Trip"
)

Could the lack of a persistent reference to CLServiceSession affect:

  1. continuation of background location updates, service session validity,
  2. or app relaunch behavior after extended background execution?

Could this behavior be related to ARC deallocating a locally scoped CLServiceSession instance after the function exits?

Thanks.

Lifecycle and Usage of CLServiceSession after the app is terminated
 
 
Q