WidgetKit + CoreData with CloudKit

Hi,
I am developing an app with CoreData + CloudKit and wanted my Widget to fetch data from CloudKit to display. Everything is working on iOS / watchOS, but on WidgetKit the result from my fetch is always empty.

I already configured the CloudKit capability on the Widget extension, then I am creating a new NSPersistentCloudKitContainer the and on

Code Block
public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {


I am trying to

Code Block
let cardsFetch = NSFetchRequest<Card>(entityName: "Card")
do {
let cards = try persistentContainer.viewContext.fetch(cardsFetch)


the result is always 0 and the same fetch on iOS returns the data.


Is this supposed to work?



Replies

Hi tbass, this should work. I would double check that all of the Core Data related files are members of both the app target and the widget target. If you still have trouble please let us know by filing an issue with Feedback Assistant!
I'm also trying to read from my CloudKit container and I don't get the CKRecord I'm supposed to get. It it supposed to work from WidgetKit?
Did anyone get this to work? I'm not sure how to load the managedObjectContext into the SwiftUI widget view. Usually we dive back to the AppDelegate to get that, but I'm guessing that this isn't possible from the widget. Can someone clarify?
Could anyone make this work? Also struggling with this...
I had the same issue and then I realized I didn't add the iCloud Capability for CloudKit in the widget extension.

After that it worked like a charm
I haven't tried this with the new widgets, but with the old widgets I found that the widget core data instance was very slow to receive updated, as in days later.

This issue was true in watchOS for a while and then it was resolved, so it may be resolved in iOS 14 too, but just something to test for.

(The workaround in my case is to store the database in a shared container)
I had the same issue too. It seems that the Path of the sqlite file is different, so what should we do if we want to add widget extension share the core data as we have had our app Production?

Hi everybody I am working on the Widget + CoreData + CloudKit

I have 2 doubts, I create the cloudKit container for main, need to create separate cloudKit container for widget? and how to fetch the data from CloudKit and use in Widget?