{"id":39205,"date":"2023-04-27T15:38:16","date_gmt":"2023-04-27T19:38:16","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=39205"},"modified":"2023-04-27T15:38:16","modified_gmt":"2023-04-27T19:38:16","slug":"apples-guidance-for-stateobject-initialization","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/04\/27\/apples-guidance-for-stateobject-initialization\/","title":{"rendered":"Apple&rsquo;s Guidance for StateObject Initialization"},"content":{"rendered":"<p><a href=\"https:\/\/www.swiftjectivec.com\/dependency-injection-with-state-object-swiftui\/\">Jordan Morgan<\/a> (<a href=\"https:\/\/mastodon.social\/@jordanmorgan\/110254498809758674\">Mastodon<\/a>):<\/p>\n<blockquote cite=\"https:\/\/www.swiftjectivec.com\/dependency-injection-with-state-object-swiftui\/\"><p>In an attempt to put an end to our <code>StateObject<\/code> woes in a more paradigmatic manner, <a href=\"https:\/\/mastodon.social\/@lucabernardi\/109948882720031817\">Luca Bernardi<\/a> let us know that <a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/stateobject\">Apple&rsquo;s official documentation<\/a> now tackles the matter. Specifically, there is now text detailing how to handle dependency injection with <code>StateObject<\/code>. This is exactly the kind of material we need from Apple, and it clears up a lot of confusion and advice I&rsquo;ve been reading.<\/p><p>[&#8230;]<\/p><p>SwiftUI only will initialize a state object the <em>first time<\/em> you call it within its view.<\/p><p>[&#8230;]<\/p><p>All of this basically boils down to - dependency injection with state object initialization works great <em>if<\/em> parties outside of the view housing it are feeding it data that doesn&rsquo;t change either.<\/p><p>[&#8230;]<\/p><p>If you did need the autoclosure to fire again, you could set the identity of the view to the value you&rsquo;re interested in. [&#8230;] Since the state object isn&rsquo;t recreated when view inputs are changed (and it certainly shouldn&rsquo;t), but when the view&rsquo;s identity changes, this route forces the initializer to run again. But again, I can&rsquo;t really see this being a viable route for most projects.<\/p><\/blockquote>\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/03\/29\/lifetime-of-state-properties-in-swiftui\/\">Lifetime of State Properties in SwiftUI<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/07\/02\/a-huge-year-for-swiftui\/\">A Huge Year for SwiftUI<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Jordan Morgan (Mastodon): In an attempt to put an end to our StateObject woes in a more paradigmatic manner, Luca Bernardi let us know that Apple&rsquo;s official documentation now tackles the matter. Specifically, there is now text detailing how to handle dependency injection with StateObject. This is exactly the kind of material we need from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2023-04-27T19:38:20Z","apple_news_api_id":"c31e258a-a82a-4549-a37d-0026254e2549","apple_news_api_modified_at":"2023-04-27T19:38:20Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/Awx4liqgqRUmjfQAmJU4lSQ","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,2185,30,2223,71,901,1812,268],"class_list":["post-39205","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-16","tag-mac","tag-macos-13-ventura","tag-programming","tag-swift-programming-language","tag-swiftui","tag-testing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/39205","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=39205"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/39205\/revisions"}],"predecessor-version":[{"id":39206,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/39205\/revisions\/39206"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=39205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=39205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=39205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}