{"id":48871,"date":"2025-08-12T15:24:48","date_gmt":"2025-08-12T19:24:48","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=48871"},"modified":"2025-08-12T15:27:29","modified_gmt":"2025-08-12T19:27:29","slug":"swiftdata-runtime","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2025\/08\/12\/swiftdata-runtime\/","title":{"rendered":"SwiftData Runtime"},"content":{"rendered":"<p><a href=\"https:\/\/stackoverflow.com\/questions\/79731020\/how-can-i-retrieve-all-swiftdata-values-at-runtime\">Richard Witherspoon<\/a>:<\/p>\n<blockquote cite=\"https:\/\/stackoverflow.com\/questions\/79731020\/how-can-i-retrieve-all-swiftdata-values-at-runtime\"><p>I&rsquo;m working with <code>SwiftData<\/code> and trying to replicate behavior similar to what I used to do with <code>CoreData<\/code>, where I had an extension on <code>NSManagedObjectContext<\/code> that allowed me to fetch all stored objects, regardless of entity type.<\/p><p>[&#8230;]<\/p><p>I&rsquo;m now using <code>SwiftData<\/code> with <code>@Model<\/code> types and the <code>ModelContext<\/code>, and I want to dynamically fetch all objects from the store at runtime, without manually specifying each type. I understand that <code>SwiftData<\/code> is designed to be more type-safe and less dynamic than <code>CoreData<\/code>, but is there any way to accomplish this without traversing through the raw SQLite file manually?<\/p><\/blockquote>\n\n<p>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 <code>Mirror<\/code> hack to get a <code>PersistentModel<\/code> from a <code>Schema.Entity<\/code>.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/05\/28\/dynamic-swift-predicates-in-macos-14-and-ios-17\/\">Dynamic Swift Predicates in macOS 14 and iOS 17<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Richard Witherspoon: I&rsquo;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.[&#8230;]I&rsquo;m now using SwiftData with @Model types and the ModelContext, and I want to dynamically fetch all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2025-08-12T19:24:50Z","apple_news_api_id":"39c2889d-224a-46fe-ac66-9e2def5cc7a5","apple_news_api_modified_at":"2025-08-12T19:26:54Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AOcKInSJKRv6sZp4t71zHpQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[109,31,2741,30,2742,71,901,943,2404],"class_list":["post-48871","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-coredata","tag-ios","tag-ios-26","tag-mac","tag-macos-tahoe-26","tag-programming","tag-swift-programming-language","tag-swift-runtime","tag-swiftdata"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/48871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=48871"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/48871\/revisions"}],"predecessor-version":[{"id":48879,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/48871\/revisions\/48879"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=48871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=48871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=48871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}