I have an App Group shared between my app and its widgets.
I have SwiftData's @Model, updated from the widget intent's perform() method and displayed on the app and the widget.
@MainActor
func perform() async throws -> some IntentResult {
let context = try ModelContainer(for: MyModel.self).mainContext
let myModel = try ModelContainer(for: MyModel.self).mainContext.fetch(
FetchDescriptor<MyModel>(predicate: #Predicate {
// The predicate is not the problem.
})
).first
myModel?.functionThatModifiesMyModel()
return .result()
}
Both the app and the widget SwiftUI views access to the model using Macros.
@Environment(\.modelContext) var context
@Query(sort: \.whatever) var myModel: [MyModel]
But the data is never correct in the app code (the widget's data is updated correctly using the intent).
Why doesn't the model make it to the app?