From 9d2e2975ebf07fc1c1118b85c253673bbb52545e Mon Sep 17 00:00:00 2001 From: Anton Gubarenko Date: Sun, 8 Feb 2026 15:28:10 +0300 Subject: [PATCH 1/4] Patter suggestion --- swiftui-expert-skill/references/state-management.md | 1 + 1 file changed, 1 insertion(+) diff --git a/swiftui-expert-skill/references/state-management.md b/swiftui-expert-skill/references/state-management.md index 27898a8..5e2082d 100644 --- a/swiftui-expert-skill/references/state-management.md +++ b/swiftui-expert-skill/references/state-management.md @@ -152,6 +152,7 @@ struct GoodView: View { ### @StateObject instantiation in View's initializer +This approach is an anti-pattern in general. Prefer storing the StateObject in the parent view or wherever the model is actually owned, then pass it down (as an StateObject direct or as binding) to keep ownership and lifecycle explicit. If you need to create a @StateObject with initialization parameters in your view's custom initializer, be aware of redundant allocations and hidden side effects. Kudos to Vincent Pradeilles. ```swift From 79a349f350fb96cd4375dc72acf55e4dae985d9d Mon Sep 17 00:00:00 2001 From: Anton Gubarenko Date: Sun, 8 Feb 2026 15:45:20 +0300 Subject: [PATCH 2/4] More clear suggestion for Child Views --- swiftui-expert-skill/references/state-management.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swiftui-expert-skill/references/state-management.md b/swiftui-expert-skill/references/state-management.md index 5e2082d..6d94461 100644 --- a/swiftui-expert-skill/references/state-management.md +++ b/swiftui-expert-skill/references/state-management.md @@ -152,7 +152,7 @@ struct GoodView: View { ### @StateObject instantiation in View's initializer -This approach is an anti-pattern in general. Prefer storing the StateObject in the parent view or wherever the model is actually owned, then pass it down (as an StateObject direct or as binding) to keep ownership and lifecycle explicit. +This approach is an anti-pattern in general. Prefer storing the StateObject in the parent view or wherever the model is actually owned, then pass it down (use @ObservedObject, @EnvironmentObject, or @Bindable (for @Observable)) to keep ownership and lifecycle explicit. If you need to create a @StateObject with initialization parameters in your view's custom initializer, be aware of redundant allocations and hidden side effects. Kudos to Vincent Pradeilles. ```swift From 3d9f69fcd2f75ef6b5ace4ebfae90e61c34688cd Mon Sep 17 00:00:00 2001 From: Anton Gubarenko Date: Mon, 16 Feb 2026 15:44:21 +0300 Subject: [PATCH 3/4] Update swiftui-expert-skill/references/state-management.md Co-authored-by: Antoine van der Lee <4329185+AvdLee@users.noreply.github.com> --- swiftui-expert-skill/references/state-management.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swiftui-expert-skill/references/state-management.md b/swiftui-expert-skill/references/state-management.md index 6d94461..1ce2d84 100644 --- a/swiftui-expert-skill/references/state-management.md +++ b/swiftui-expert-skill/references/state-management.md @@ -153,7 +153,7 @@ struct GoodView: View { ### @StateObject instantiation in View's initializer This approach is an anti-pattern in general. Prefer storing the StateObject in the parent view or wherever the model is actually owned, then pass it down (use @ObservedObject, @EnvironmentObject, or @Bindable (for @Observable)) to keep ownership and lifecycle explicit. -If you need to create a @StateObject with initialization parameters in your view's custom initializer, be aware of redundant allocations and hidden side effects. Kudos to Vincent Pradeilles. +If you need to create a @StateObject with initialization parameters in your view's custom initializer, be aware of redundant allocations and hidden side effects. ```swift // WRONG - creates a new ViewModel instance each time the view's initializer is called From c062f0b1e9fdd02772cb7efad29cf6bee2fb7833 Mon Sep 17 00:00:00 2001 From: Anton Gubarenko Date: Tue, 17 Feb 2026 19:04:03 +0300 Subject: [PATCH 4/4] Comments added --- swiftui-expert-skill/references/state-management.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swiftui-expert-skill/references/state-management.md b/swiftui-expert-skill/references/state-management.md index 1ce2d84..5dc503f 100644 --- a/swiftui-expert-skill/references/state-management.md +++ b/swiftui-expert-skill/references/state-management.md @@ -150,7 +150,7 @@ struct GoodView: View { } ``` -### @StateObject instantiation in View's initializer +### @StateObject instantiation in View's initializer (if it's a Parent view) This approach is an anti-pattern in general. Prefer storing the StateObject in the parent view or wherever the model is actually owned, then pass it down (use @ObservedObject, @EnvironmentObject, or @Bindable (for @Observable)) to keep ownership and lifecycle explicit. If you need to create a @StateObject with initialization parameters in your view's custom initializer, be aware of redundant allocations and hidden side effects.