(My) associated drawback (see feedback): SwiftUI – Share SwiftData View with Widget
Swift hobbyist…Be happy to level out how unhealthy the code is.
I am attempting to construct a SwiftUI app and I’ve a SwiftData mannequin known as Diem (a countdown occasion class). The mannequin is linked to DynamicAppIntent in that the person ought to be capable to select from one of many countdowns within the mannequin to show on the widget. Nevertheless, each time I fetch from the mannequin Xcode provides me a FatalError. See under for particulars.
The log:
Loading diems to recommend for particular diem...
Discovered 4 diems
Loading diems for identifiers: ["to"]
SwiftData/DataUtilities.swift:65: Deadly error: Could not discover 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(title: "date", keypath: Diem.date, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(title: "element", keypath: Diem.element, defaultValue: nil, metadata: nil)]
The Question for the entity/intent.
struct DiemEntityQuery: EntityQuery, Sendable {
func entities(for identifiers: [DiemEntity.ID]) async throws -> [DiemEntity] {
logger.information("Loading diems for identifiers: (identifiers)")
let container = attempt ModelContainer(for: Diem.self)
let modelContext = ModelContext(container)
// VVV FATALERROR VVV
let diems = attempt modelContext.fetch(FetchDescriptor(predicate: #Predicate { identifiers.incorporates($0.id) })) // FATAL ERROR
// AAA FATALERROR AAA
logger.information("Discovered (diems.rely) diems")
return diems.map { DiemEntity(from: $0) }
}
func suggestedEntities() async throws -> [DiemEntity] {
logger.information("Loading diems to recommend for particular diem...")
let container = attempt ModelContainer(for: Diem.self)
let modelContext = ModelContext(container)
let diems = attempt modelContext.fetch(FetchDescriptor())
logger.information("Discovered (diems.rely) diems")
return diems.map { DiemEntity(from: $0) }
}
}
The total challenge is on GitHub if anybody would care to spend their valuable time to test it out…I have been caught on this drawback for months and would REALLY recognize some help.
The issue occurs with each iOS 17 and 18. I’ve tried including an specific id to the Diem mannequin however with that the mannequin could not even initialize…
This may be an issue with my Supplier? Then once more I do not discover any tutorials on-line that explains find out how to use DynamicAppIntents with SwiftData…
@pawello2222’s WidgetExamples have been very useful. I simply want pawellow would add one other instance combining each SwiftData and DynamicAppIntent. I’ve additionally referenced Apple’s personal BackyardBirds however the SwiftData Intent does not appear to work and somethings appears to be dealing with the issue?
Thanks!