Background tasks to loaded updated products

We are building an App that collects Products from an online database. During setup of the App the initial products data set is loaded and saved into the SwiftData Database.

Over time the product information will change in the online environment and we need to update this information accordingly in the App.

The idea is that based on the product update date (timestamp) we will collect all the products that have been updated since the last create/update sync.

Because it can be a large amount of products that need to be updated we would like to execute this update task in the background preferably during the night when the App/Device is not used.

We found the following from Apple and tried to implement this, https://developer.apple.com/wwdc22/10142

We would like to use this SwiftUI background tasks feature to accommodate this but are running in some questions:

  • Would the Backgroundtasks be the right approach to implement this feature?
  • Can we trust that this update will be executed on the defined schedule
  • Can we trust that this update will be completed once started?

"Can we trust that this update will be executed on the defined schedule". Absolutely not, read the documentation more closely. It'll probably run, but you can't specify when, nor be sure it will. You can also just update it when the user loads the app? Surely it's not going to take hours to download, or even minutes, even if there's a lot of data. You can start a background thread when the user launches the app so it wouldn't hang the ui.

Would the Backgroundtasks be the right approach to implement this feature?

Yes.

Can we trust that this update will be executed on the defined schedule

No. Ultimately all background execution is at the discretion of the system, and there will be circumstances under which it doesn’t happen. However, a background processing request (BGProcessingTaskRequest) is typically honoured overnight.

Can we trust that this update will be completed once started?

No. That’s why it has an expiration handler (expirationHandler).

I have a bunch of links to additional docs in Background Tasks Resources. If nothing else, watch Session 10063.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Background tasks to loaded updated products
 
 
Q