mentrena / SyncKit

Automatic CloudKit synchronization
https://mentrena.github.io/SyncKit/
MIT License
507 stars 59 forks source link

How to get shared data in WidgetExtension? #168

Closed Mr-ZNN closed 3 years ago

Mr-ZNN commented 3 years ago

In the app target, I can use the following code to get the received shared data.

if let sharedSynchronizer = (UIApplication.shared.delegate as? AppDelegate)?.sharedSynchronizer, let resultsController: MultiRealmResultsController<Event> = sharedSynchronizer.multiRealmResultsController() {
    let eventGroups: [[Event]] = resultsController.results.map({ Array($0) })
}

So how to get it in the widget?

mentrena commented 3 years ago

Sharing data with a widget extension is out of the scope of what SyncKit does. It is something you need to design yourself in your app, but generally you will want to create an app group and store your data in the app group container, or even better you would keep the main store in your app, and create a dedicated store in the app group just to share a smaller subset of the data with the widget extension.

https://useyourloaf.com/blog/sharing-data-with-a-widget/

https://michael-kiley.medium.com/sharing-object-data-between-an-ios-app-and-its-widget-a0a1af499c31