I am having a lot of trouble reliably making network requests from the DeviceActivityMonitor extension.
In the app extension, when intervalDidEnd
is called by the system, I add some data to the app's local CoreData store and sync that with the server. I use URLSession dataTask
to make a POST request to my server, with URLSessionConfiguration.sharedContainerIdentifier
set to my App Group's shared identifier. It is extremely rare that my server receives this request, although sometimes (rarely) it does.
I've tried things like wrapping the task in performExpiringActivity or using URLSessionConfiguration.background
suspecting that this task needs to run in the background, but none of it seems to work.
What I suspect is happening is that the App Extension gets cancelled before the request is able to be invoked. Looking at the console logs, sometimes the work stops before the request is called, sometimes during the request task, and sometimes even before I am able to update my local data store.
It's been very difficult to find any information on the lifecycle of the DeviceActivityMonitor Extension. What I want to know is:
- How can I reliably make network requests from the DeviceActivityMonitor Extension?
- What is the lifecycle of this extension? (How long do I have to complete work inside callback functions invoked by the system? And can I extend this time if necessary?)