(My) related problem (see comments): SwiftUI - Share SwiftData View with Widget
Swift hobbyist...Feel free to point out how bad the code is.
I'm trying to build a SwiftUI app and I have a SwiftData model called Diem (a countdown event class). The model is linked to DynamicAppIntent in that the user should be able to choose from one of the countdowns in the model to display on the widget. However, every time I fetch from the model Xcode gives me a FatalError. See below for details.
The log:
Loading diems to suggest for specific diem...
Found 4 diems
Loading diems for identifiers: ["to"]
SwiftData/DataUtilities.swift:65: Fatal error: Couldn't find \Diem.id on Diem with fields [SwiftData.Schema.PropertyMetadata(name: "name", keypath: \Diem.name, defaultValue: nil, metadata: Optional(Attribute - name: , options: [unique], valueType: Any, defaultValue: nil, hashModifier: nil)), SwiftData.Schema.PropertyMetadata(name: "date", keypath: \Diem.date, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "detail", keypath: \Diem.detail, defaultValue: nil, metadata: nil)]
The Query for the entity/intent.
struct DiemEntityQuery: EntityQuery, Sendable {
func entities(for identifiers: [DiemEntity.ID]) async throws -> [DiemEntity] {
logger.info("Loading diems for identifiers: \(identifiers)")
let container = try ModelContainer(for: Diem.self)
let modelContext = ModelContext(container)
// VVV FATALERROR VVV
let diems = try modelContext.fetch(FetchDescriptor<Diem>(predicate: #Predicate { identifiers.contains($0.id) })) // FATAL ERROR
// AAA FATALERROR AAA
logger.info("Found \(diems.count) diems")
return diems.map { DiemEntity(from: $0) }
}
func suggestedEntities() async throws -> [DiemEntity] {
logger.info("Loading diems to suggest for specific diem...")
let container = try ModelContainer(for: Diem.self)
let modelContext = ModelContext(container)
let diems = try modelContext.fetch(FetchDescriptor<Diem>())
logger.info("Found \(diems.count) diems")
return diems.map { DiemEntity(from: $0) }
}
}
The full project is on GitHub if anyone would care to spend their precious time to check it out...I've been stuck on this problem for months and would REALLY appreciate some assistance.
The problem happens with both iOS 17 and 18. I have tried adding an explicit id to the Diem model but with that the model couldn't even initialize...
This might be a problem with my Provider? Then again I don't find any tutorials online that explains how to use DynamicAppIntents with SwiftData...
@pawello2222's WidgetExamples have been very helpful. I just wish pawellow would add another example combining both SwiftData and DynamicAppIntent. I have also referenced Apple's own BackyardBirds but the SwiftData Intent doesn't seem to work and somethings seems to be handling the problem?
Thank you!
If you change the predicate to identifiers.contains($0.name)
it will no longer crash.
A type created with @Model
already has an id property (and already conforms to Identifiable
) so I don't see the point in overriding that property, it might possibly even introduce other issues if you do. So my recommendation is to delete the id property from Diem