TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Can I update a Widget on one device when a SwiftData/Cloudkit object is modified on another?

Forums > SwiftUI

I have an app which is happily syncing SwiftData across devices using the built in Cloudkit syncing. The app has a widget which also uses this data to display current information.. When this app is in the background, is it possible for its widget to receive a notifcation to update when the shared data is changed on another device?

I guess I could get the widget to update every 15 minutes, but this seems wasteful. I'd much rather try and receive a remote notification (from CloudKit?) which launches a background task to fetch the changes, and only refresh the widget timeline once this has happened..

Any help would be massively appreciated..

2      

For anyone else looking for an answer to this.. the route I'm investigating, which seems promising, is to use Cloudkit notifictaions.. so I'm subscribing to a Cloudkit notification in my private database for the entity that my widget is using to get its state from via SwiftData. This fires a remote notification and if my app is not in an active state I ask for a reload of the widget. It appears that there is no guarantee of delivery time so there can be a bit of delay. If anyone finds another solution please do add something here to this thread..

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.