I am having a complication that needs to update once in a while by fetching data from a server.
I am trying to fetch the data from the Watch. I am doing so by scheduling a WKRefreshBackgroundTask
. When this task fires, I start a URL session to fetch some JSON data from a server.
After the data has been fetched successfully, I complete the WKApplicationRefreshBackgroundTask
by calling its setTaskCompletedWithSnapshot(false)
.
This all seems to work OK-ish, however, I'd rather create a WKURLSessionRefreshBackgroundTask
after the WKApplicationRefreshBackgroundTask
to download it in the background and consume less time in the background tasks. I think this is not possible, I am not downloading or uploading files. If I understand correctly WKURLSessionRefreshBackgroundTask
with a URLSessionConfiguration.background(withIdentifier:)
can only be used a for URLSessionUploadTask
s or URLSessionDownloadTask
s, so I had to forget about that idea. (Please correct me if I misunderstood that).
Now the biggest problem I run into is that when the Watch is locked (taken off the wrist, put on the charging puck, etc.) the URL Requests are not being executed. And thus the data is not been updated. How should we deal with this? The activity compilation simply turns dark when the Watch gets locked and as soon as you unlock the Watch it reloads. Is there anything we can do to understand the Watch is locked/unlocked?
Is it even the right approach to fetch the data from the Watch using a WKApplicationRefreshBackgroundTask
, or is it better to wake up the phone and get the phone to request the data using WatchConnectivity
? And if so: which WCSessionDelegate
functions would you use to achieve that?
If fetching the data directly from the Watch is the way to go: can I set the WKApplicationRefreshBackgroundTask
to completed before the request has completed, or should I wait until after the request completed? (i.e., at what point in time should WKApplicationRefreshBackgroundTask.setTaskCompletedWithSnapshot()
be called).
I read loads and loads of articles and watched multiple videos. Yet still I find it very hard to find what the correct architecture is to fetch compilation data from a server and get this working rock solid.
Hopefully someone can shine a little light on this matter.