Tuesday, August 12, 2025

SwiftData Runtime

Richard Witherspoon:

I’m working with SwiftData and trying to replicate behavior similar to what I used to do with CoreData, where I had an extension on NSManagedObjectContext that allowed me to fetch all stored objects, regardless of entity type.

[…]

I’m now using SwiftData with @Model types and the ModelContext, and I want to dynamically fetch all objects from the store at runtime, without manually specifying each type. I understand that SwiftData is designed to be more type-safe and less dynamic than CoreData, 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:

Comments RSS · Twitter · Mastodon

Leave a Comment