Hey,
I'm building an app that uses App Intents to modify data stored in SwiftData and synced through CloudKit.
My expectation is that these changes should eventually be reflected in my app's widgets across all of the user's devices (iPhone, iPad, and Mac). However, I'm struggling to find a reliable way to ensure widgets reload when the underlying data changes as a result of a CloudKit sync.
Reloading widgets on the device that modifies the data works reliably. The challenge is ensuring widgets reload on other devices after the updated data has been synced through CloudKit. In practice, this doesn't appear to happen reliably.
I do not expect the user to manually open the app on those devices. I'm fine with the system launching my app in the background if that's part of the intended solution.
Is there a recommended approach for keeping widgets in sync with SwiftData and CloudKit across devices?
More specifically:
- Can WidgetKit be notified when SwiftData receives updates from CloudKit?
- Is there any supported way to trigger widget reloads on remote devices after a CloudKit sync?
- If not, what is the recommended architecture for ensuring widgets stay reasonably up to date when synchronized data changes?
Things I've considered and/or tried:
-
Calling sendChanges(_:) on
CKSyncEnginefrom my App Intent to push changes immediately. However, this depends on an active Internet connection and doesn't address the case where changes are made offline and synchronized to CloudKit at a later time. -
Sending push notifications to the user's devices after an App Intent runs and using WidgetPushHandler to reload widgets. However, this requires confidence that the changes have already been uploaded to CloudKit. As far as I can tell, that's difficult or impossible to guarantee in the general case because of the issue described above.
-
Calling fetchChanges(_:) on
CKSyncEnginefrom the widget's timeline provider to ensure the widget has the latest data. However, the widget first needs some indication that new changes are available in CloudKit. Additionally, widget timeline reloads appear to have fairly strict execution time limits, which makes performing CloudKit synchronization work in that context seem less than ideal.
My goal is for a Shortcut run on one device to update data and have widgets on all of the user's devices reflect those changes without requiring the user to manually open the app on each device.
Am I thinking about this problem correctly, or is there a recommended pattern I'm missing?
I'd appreciate any guidance on the intended WidgetKit + SwiftData + CloudKit integration story for this scenario. Thanks!