{"id":49034,"date":"2025-08-26T14:57:45","date_gmt":"2025-08-26T18:57:45","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=49034"},"modified":"2025-08-26T15:00:41","modified_gmt":"2025-08-26T19:00:41","slug":"swiftdatas-modelactor-is-just-weird","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2025\/08\/26\/swiftdatas-modelactor-is-just-weird\/","title":{"rendered":"SwiftData&rsquo;s ModelActor Is Just Weird"},"content":{"rendered":"<p><a href=\"https:\/\/www.massicotte.org\/model-actor\">Matt Massicotte<\/a> (<a href=\"https:\/\/mastodon.social\/@mattiem\/114206038393350547\">Mastodon<\/a>):<\/p>\n<blockquote cite=\"https:\/\/www.massicotte.org\/model-actor\"><p>So, no doubt there&rsquo;s lots of historical stuff going on here.<\/p><p>But, that still doesn&rsquo;t explain how much <a href=\"https:\/\/brightdigit.com\/tutorials\/swiftdata-modelactor\/\">trouble<\/a> people have with <a href=\"https:\/\/developer.apple.com\/documentation\/swiftdata\/modelactor\">ModelActor<\/a>. I&rsquo;m not sure <strong>anyone<\/strong> has ever used <code>ModelActor<\/code> without at least some surprises.<\/p><p>[&#8230;]<\/p><p>Actors exist to protect mutable state. The purpose of a <code>ModelActor<\/code> is to own and isolate the <code>ModelContext<\/code>. It does that! But if we start to dig into <strong>how<\/strong> exactly it does it, we will discover something very bizarre.<\/p><p>[&#8230;]<\/p><p>Somehow, we are on our custom, minimal, SwiftData-defined actor <strong>and<\/strong> also the <code>MainActor<\/code> at the same time.<\/p><p>[&#8230;]<\/p><p>It is bad because consumers of this API have a very reasonable expectation that this will execute off the main thread. This type doesn&rsquo;t do that. But worse, its relationship with the main thread isn&rsquo;t visible in the type system. These things are <strong>not<\/strong> marked <code>MainActor<\/code>, so the compiler doesn&rsquo;t know what&rsquo;s going on. This means even though you are on the main thread here, you cannot access <code>MainActor<\/code> stuff.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/mastodon.social\/@mattiem\/115028907993962755\">Matt Massicotte<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@mattiem\/115028907993962755\">\n<p>Anyone know if SwiftData&rsquo;s <code>ModelActor<\/code> still has weird concurrency behavior in OS 26?<\/p>\n<p>[&#8230;]<\/p>\n<p>Based on some limited testing, no, not fixed. <code>ModelActor<\/code> types can still ultimately execute on the main thread, depending on calling context.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/mastodon.social\/@vanvoorden\/115029413486804480\">Rick van Voorden<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@vanvoorden\/115029413486804480\"><p>AFAIK the legit workaround will continue to be to ensure the <code>ModelActor<\/code> is created off <code>main<\/code>. Which leads to workarounds like what we do in <code>ImmutableData<\/code> sample products when we &ldquo;box&rdquo; the <code>ModelActor<\/code> with a <code>lazy<\/code> property in <em>another<\/em> actor that <em>is<\/em> created on <code>main<\/code>.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/mastodon.social\/@mattiem\/115029429535580393\">Matt Massicotte<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@mattiem\/115029429535580393\">\n<p>Someone proved that init off main is insufficient. I have a theory on what&rsquo;s happening, and I think this workaround you suggest will always work. But yeah I&rsquo;m hoping this all just goes away.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2025\/08\/07\/sendable-unchecked-sendable-sendable-sending-and-nonsending\/\">Sendable, @unchecked Sendable, @Sendable, sending, and nonsending<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2025\/07\/23\/ways-swiftdatas-modelcontainer-can-error-on-creation\/\">Ways SwiftData&rsquo;s ModelContainer Can Error on Creation<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2025\/06\/19\/swiftdata-and-core-data-at-wwdc25\/\">SwiftData and Core Data at WWDC25<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/05\/12\/mainactor-not-guaranteed\/\">@MainActor Not Guaranteed<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Matt Massicotte (Mastodon): So, no doubt there&rsquo;s lots of historical stuff going on here.But, that still doesn&rsquo;t explain how much trouble people have with ModelActor. I&rsquo;m not sure anyone has ever used ModelActor without at least some surprises.[&#8230;]Actors exist to protect mutable state. The purpose of a ModelActor is to own and isolate the ModelContext. [&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-26T18:57:48Z","apple_news_api_id":"2f25ea34-e0ee-4935-aa12-7e7e1883a5b3","apple_news_api_modified_at":"2025-08-26T19:00:44Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/ALyXqNODuSTWqEn5-GIOlsw","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":[31,2586,2741,30,2598,2742,71,2200,901,2404],"class_list":["post-49034","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-18","tag-ios-26","tag-mac","tag-macos-15-sequoia","tag-macos-tahoe-26","tag-programming","tag-swift-concurrency","tag-swift-programming-language","tag-swiftdata"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/49034","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=49034"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/49034\/revisions"}],"predecessor-version":[{"id":49041,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/49034\/revisions\/49041"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=49034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=49034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=49034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}