Good morning everyone!
Today I have a question about using SwiftData with CloudKit and Widgets. I recently set up my project for SwiftData and CloudKit synchronization, but for some reason, I’m not able to give my Widget access to this data. CloudKit works perfectly fine for my main app, but the Widget only shows placeholder data(the placeholder data which were defined in the get functions as catch, this is sure).
I have set the CloudKit capability for my Widget extension and tried fetching data with the get-functions in the code below. I also ensured that the data model files are members of the Widget extension target and that the Widget extension uses the same CloudKit container as the main app.
I wondered if it is possible and reasonable to save a copy of my CloudKit data in an App Group container, but in that case, the information shown in the Widget are not always up-to-date, so a solution that fetches data directly from CloudKit would be better. Has anyone had experience with this case? I couldn’t find much information about this problem online.
In the code below, many parts have been deleted or altered because they are not relevant to the problem, as they don’t fetch data. The variables, functions, and data models in the code may sometimes have German names, but I hope you can still understand it.
Thanks for your help!
struct Provider: AppIntentTimelineProvider {
//[Placeholder and snapshot]
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<CleverEntry> {
let entry = await loadAllVariables()
return Timeline(entries: [entry], policy: .after(Date().addingTimeInterval(60 * 5)))
}
@MainActor
private func getExam() -> [PruefungM] {
//Old, local version
/*
guard let modelContainer = try? ModelContainer(for: PruefungM.self) else {
return []
}
let descriptor = FetchDescriptor<PruefungM>()
let PRF = try? modelContainer.mainContext.fetch(descriptor)
return PRF ?? []
*/
do {
let configuration = ModelConfiguration(cloudKitDatabase: .private("iCloud.my_bundle_id"))
let container = try ModelContainer(
for: PruefungM.self,
configurations: configuration
)
let descriptor = FetchDescriptor<PruefungM>()
return try container.mainContext.fetch(descriptor)
} catch {
print("❌ Error(CloudKit): \(error)")
return []
}
}
@MainActor
private func getHAF() -> [HausaufgabeM] {
do {
let configuration = ModelConfiguration(cloudKitDatabase: .private("iCloud.my_bundle_id"))
let container = try ModelContainer(
for: HausaufgabeM.self,
configurations: configuration
)
let descriptor = FetchDescriptor<HausaufgabeM>()
return try container.mainContext.fetch(descriptor)
} catch {
print("❌ Error (CloudKit): \(error)")
return []
}
}
@MainActor
private func loadAllVariables() -> CleverEntry {
print("Function started")
let HAF = getHAF()
let PRF = getExam()
//handling and returning the data
}
}
An app and its extensions can share a SwiftData store located in a shared App Group container. The following Apple sample demonstrates that:
The discussion in this post may help as well, if you have any data update issue.
CloudKit integration adds more complexity on this topic. You can start with checking if this post helps. SwiftData + CloudKit uses NSPersistentCloudKitContainer
under the hood, and so the content applies.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.