Ran into this as well. The compiler is optimizing something incorrectly for release builds. Using Predicate init directly (instead of the Macro) doesn't resolve the issue. Nor does any combo of explicit/verbose casting.
Something as simple as this:
func fetchModel<M>(with id: UUID, prefetching relationshipKeyPaths: [PartialKeyPath<M>] = []) throws -> M where M: PersistentModel, M.ID == UUID {
var fetchDescriptor = FetchDescriptor<M>()
fetchDescriptor.relationshipKeyPathsForPrefetching = relationshipKeyPaths
fetchDescriptor.predicate = #Predicate { $0.id == id }
fetchDescriptor.fetchLimit = 1
guard let model: M = try? modelContext.fetch(fetchDescriptor).first else {
throw SwiftDataRepoError.modelNotFound(withId: id)
}
return model
}
Results in this crash:
SwiftData/DataUtilities.swift:85: Fatal error: Couldn't find \Person.<computed 0x0000000100d6511c (UUID)> on Person with fields [SwiftData.Schema.PropertyMetadata(name: "id", keypath: \Person.<computed 0x0000000100d8da8c (UUID)>, defaultValue: Optional(5CAE942A-EF4E-4B89-A777-D79153C7F276), metadata: nil) ....
Only thing that works is initializing FetchDescriptor with the explicit type.
var fetchDescriptor = FetchDescriptor<Person>()
fetchDescriptor.relationshipKeyPathsForPrefetching = relationshipKeyPaths
fetchDescriptor.predicate = #Predicate { $0.id == id }
fetchDescriptor.fetchLimit = 1
guard let model = try modelContext.fetch(fetchDescriptor).first else {
throw SwiftDataRepoError.modelNotFound(withId: id)
}
return model
Going to mess with some compiler settings to try and narrow this down. This might need to be reported to the SwiftLang/Foundation issue tracker, it may not be a bug with SwiftData per se.
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: