{"id":35407,"date":"2022-03-29T13:43:54","date_gmt":"2022-03-29T17:43:54","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=35407"},"modified":"2022-03-29T13:43:54","modified_gmt":"2022-03-29T17:43:54","slug":"lifetime-of-state-properties-in-swiftui","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/03\/29\/lifetime-of-state-properties-in-swiftui\/","title":{"rendered":"Lifetime of State Properties in SwiftUI"},"content":{"rendered":"<p><a href=\"https:\/\/chris.eidhof.nl\/post\/swiftui-state-lifetime\/\">Chris Eidhof<\/a>:<\/p>\n<blockquote cite=\"https:\/\/chris.eidhof.nl\/post\/swiftui-state-lifetime\/\">\n<p>However, when you have worked with <code>@State<\/code> (or <code>@StateObject<\/code>), you will notice that there might be some strange behavior. Sometimes your state disappears, especially when working with a <code>List<\/code> (or to be precise: any view that uses <code>ForEach<\/code> directly or indirectly).<\/p>\n<p>[&#8230;]<\/p>\n<p><em>List creates its subviews lazily<\/em>.<\/p>\n<p>[&#8230;]<\/p>\n<p><em>The children of a <code>List<\/code> will be kept around<\/em>. The lifetime of a <code>@State<\/code>&rsquo;s property is directly tied to the lifetime of a view. Once a list child is created, it never goes away again, unless the list goes away itself.<\/p>\n<p>[&#8230;]<\/p>\n<p>It turns out that while the children of a <code>List<\/code> will be kept around (including their associated state), the bodies of those views will get destroyed. These will get recreated lazily once the view appears on screen again.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Chris Eidhof: However, when you have worked with @State (or @StateObject), you will notice that there might be some strange behavior. Sometimes your state disappears, especially when working with a List (or to be precise: any view that uses ForEach directly or indirectly). [&#8230;] List creates its subviews lazily. [&#8230;] The children of a List [&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":"2022-03-29T17:43:56Z","apple_news_api_id":"7efbf143-ce60-48d0-a11d-09e220b5b012","apple_news_api_modified_at":"2022-03-29T17:43:57Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AfvvxQ85gSNChHQniILWwEg","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,2078,30,2077,571,71,1812],"class_list":["post-35407","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-15","tag-mac","tag-macos-12","tag-memory-management","tag-programming","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35407","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=35407"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35407\/revisions"}],"predecessor-version":[{"id":35408,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35407\/revisions\/35408"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=35407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=35407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=35407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}