SwiftData Runtime
I’m working with
SwiftData
and trying to replicate behavior similar to what I used to do withCoreData
, where I had an extension onNSManagedObjectContext
that allowed me to fetch all stored objects, regardless of entity type.[…]
I’m now using
SwiftData
with@Model
types and theModelContext
, and I want to dynamically fetch all objects from the store at runtime, without manually specifying each type. I understand thatSwiftData
is designed to be more type-safe and less dynamic thanCoreData
, but is there any way to accomplish this without traversing through the raw SQLite file manually?
This sort of metaobject stuff is really easy to do with Core Data, using public APIs available from the very first version. Currently, with SwiftData you need a fragile Mirror
hack to get a PersistentModel
from a Schema.Entity
.
Previously: