Run Api code even when killed the App

We are developing a solution, in which we need to send user's current location to server and connect to socket. It will be a continuous process when app is in kill mode.We achieved to get the user location when app is in kill mode, but Can we hit the API continuously when app is in kill mode?

Answered by Engineer in 795969022

If the app is killed, there is no mechanism that will allow you to keep receiving locations continuously as before so that you can keep a socket connection open uninterrupted. You can get close, but not exactly what you describe you want.

You say you achieved to get some location updates after the app was killed. I am not sure which API you are using, but that is achievable via the low power monitoring APIs like CLMonitor, Region Monitoring, Visits, or Significant Location updates - which will send location updates to your app even after it has been killed - but requires the device to move a certain amount or to certain locations depending on the specifics. So, even if you were able to start the continuous location updates after the app being relaunched in the background by one of these APIs, there will be a gap between the app's termination and it's subsequent relaunch when it meets the conditions specified for these APIs to start working.

Your other option is to use the high power APIs like liveUpdates() or startUpdatingLocation(). These will give you continuous location updates with some nuances.

startUpdatingLocation() will give you updates every second whether the device is moving or not, so you can keep a socket open as well. But if the app is terminated you cannot continue receiving updates until the user launches the app to start location updates again.

CLLocationUpdate.liveUpdates() will restart location updates after the app is terminated, but also only send updates if the device is moving. The updates will stop if the device is stationary, which will suspend your app and consequentially drop your socket connection. But it will restart updates as soon as the device starts moving, so you can reopen a socket connection and continue your updates.

So, you have a number of options here, and you can choose the API that is most suitable for your use case. You will just need to adjust your expectations and understand that interruptions will happen and cannot be avoided, so you may need to alter your app's architecture accordingly.


Argun Tekant /  DTS Engineer / Core Technologies

If the app is killed, there is no mechanism that will allow you to keep receiving locations continuously as before so that you can keep a socket connection open uninterrupted. You can get close, but not exactly what you describe you want.

You say you achieved to get some location updates after the app was killed. I am not sure which API you are using, but that is achievable via the low power monitoring APIs like CLMonitor, Region Monitoring, Visits, or Significant Location updates - which will send location updates to your app even after it has been killed - but requires the device to move a certain amount or to certain locations depending on the specifics. So, even if you were able to start the continuous location updates after the app being relaunched in the background by one of these APIs, there will be a gap between the app's termination and it's subsequent relaunch when it meets the conditions specified for these APIs to start working.

Your other option is to use the high power APIs like liveUpdates() or startUpdatingLocation(). These will give you continuous location updates with some nuances.

startUpdatingLocation() will give you updates every second whether the device is moving or not, so you can keep a socket open as well. But if the app is terminated you cannot continue receiving updates until the user launches the app to start location updates again.

CLLocationUpdate.liveUpdates() will restart location updates after the app is terminated, but also only send updates if the device is moving. The updates will stop if the device is stationary, which will suspend your app and consequentially drop your socket connection. But it will restart updates as soon as the device starts moving, so you can reopen a socket connection and continue your updates.

So, you have a number of options here, and you can choose the API that is most suitable for your use case. You will just need to adjust your expectations and understand that interruptions will happen and cannot be avoided, so you may need to alter your app's architecture accordingly.


Argun Tekant /  DTS Engineer / Core Technologies

Run Api code even when killed the App
 
 
Q