WKRefreshBackgroundTask for CloudKit on watchOS?

301 views Asked by At

So, on watchOS, I know you can schedule (and reschedule) a WKRefreshBackgroundTask to do work for you in the background. This is great for my app to make sure that data is current and up-to-date on watchOS - especially since it doesn't receive updates like iOS and macOS.

Except, my data uses CloudKit to store a user's data.

I want to use something like the URLSession scenario, where you schedule a WKApplicationRefreshBackgroundTask where you create a background URLSession task and hand it to the system. The system in turn hands you a WKURLSessionRefreshBackgroundTask where you check if your data completed its URLSession (by checking if the downloaded data exists).

My question is, how do you do something like this with CloudKit?

I could just do the downloading of my user's data in the WKApplicationRefreshBackgroundTask, but I don't think I'm guaranteed it will finish in the time the system gives to my app. This is why (it seems) Apple recommends you split this into two Background Tasks:

  1. WKApplicationRefreshBackgroundTask - to create the URLSession and hand it to the system.
  2. WKURLSessionRefreshBackgroundTask - to act upon the data downloaded from step 1.

I am not sure you can do this with CloudKit? Or, at least I can't seem to find something of the sort.

1

There are 1 answers

2
Marc T. On

If you want to use WKURLSessionRefreshBackgroundTask you have to access the iCloud database via URLSession. This is possible from an app but much more complicated.

Don't forget to get your API request token for your database on the iCloud dashboard if you want to try it out.