UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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..


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..


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.