# Why does this not work

**URL:** <https://forums.kodeco.com/t/why-does-this-not-work/179447>\
**Category:** SwiftUI by Tutorials\
**Created:** [October 4, 2022, 2:47pm UTC](https://forums.kodeco.com/t/why-does-this-not-work/179447 "2022-10-04T14:47:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tchelyzt](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tchelyzt/32/111572_2.png) [@tchelyzt](https://forums.kodeco.com/u/tchelyzt)\
**Post date:** [October 4, 2022, 2:47pm UTC](https://forums.kodeco.com/t/why-does-this-not-work/179447/1 "2022-10-04T14:47:09Z")

</div>

I have an app that depends on a model which displays quite different screens depending on current state. Here’s a simple minimum example WHICH WORKS to illustrate the idea. The model is observed via @StateObject and the ContentView() simply switches on state.

In my real model, each display is more complex so it makes sense to drop them into separate structs, but when I do, they seem to lose track of state. This is also illustrated below in my very simple example. What am I doing wrong?

**Working Version of ContentView()**

```
struct ContentView: View {
   @StateObject var viewModel = ViewModel()

   var body: some View {
      Group {
         switch viewModel.state {
            case .A: VStack {
               Text("Showing A")
               Button("Change") {
                  viewModel.changeState(.B)
               }
            }
            case .B: VStack {
               Text("Showing B")
               Button("Change") {
                  viewModel.changeState(.C)
               }
            }
            case .C: VStack {
               Text("Showing C")
               Button("Change") {
                  viewModel.changeState(.A)
               }
            }
         }
      }.environmentObject(viewModel)
   }
}

```

**Broken Version of ContentView()**

```
struct ContentView: View {
   @StateObject var viewModel = ViewModel()

   var body: some View {
      Group {
         switch viewModel.state {
            case .A: AView()
            case .B: BView()
            case .C: CView()
         }
      }.environmentObject(viewModel)
   }
}

struct AView: View {
   @StateObject var viewModel = ViewModel()

   var body: some View {
      VStack {
         Text("Showing A")
         Button("Change") {
            viewModel.changeState(.B)
         }
      }
   }
}

struct BView: View {
   @StateObject var viewModel = ViewModel()
   
   var body: some View {
      VStack {
         Text("Showing B")
         Button("Change") {
            viewModel.changeState(.C)
         }
      }
   }
}

struct CView: View {
   @StateObject var viewModel = ViewModel()
   
   var body: some View {
      VStack {
         Text("Showing C")
         Button("Change") {
            viewModel.changeState(.A)
         }
      }
   }
}

```

**The Model and the ViewModel:**

```
struct Model {
   var state:State = .A
}

enum State: String {
   case A
   case B
   case C
}

class ViewModel: ObservableObject {
   @Published var model = Model()
   var state: State { return model.state }
   
   func changeState(_ state: State) {
      model.state = state
   }
}

```

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [October 4, 2022, 10:03pm UTC](https://forums.kodeco.com/t/why-does-this-not-work/179447/2 "2022-10-04T22:03:07Z")

</div>

hi Don, to use an environment object in your subviews, declare it as `@EnvironmentObject` not `@StateObject`, and don’t reinitialise it:

```auto
@EnvironmentObject var history: HistoryStore

```

---

<div class="post-metadata">

**Author:** ![tchelyzt](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tchelyzt/32/111572_2.png) [@tchelyzt](https://forums.kodeco.com/u/tchelyzt)\
**Post date:** [October 4, 2022, 11:17pm UTC](https://forums.kodeco.com/t/why-does-this-not-work/179447/3 "2022-10-04T23:17:51Z")

</div>

Thanks Audrey  
Sorry … elementary error. However, I didn’t make this error in my real app so I’ve failed to capture the essence of what’s going on in my simplified one. Back to the drawing board!  
Tchelyzt

---

<div class="post-metadata">

**Author:** ![jeden](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jeden/32/110971_2.png) [@jeden](https://forums.kodeco.com/u/jeden)\
**Post date:** [November 17, 2022, 12:34pm UTC](https://forums.kodeco.com/t/why-does-this-not-work/179447/4 "2022-11-17T12:34:15Z")

</div>

Have you figured out where the problem is?

If not, is it possible that you’re triggering the update from a background thread, or from a view that’s not in the hierarchy of the view which should be affected by the change?

---

<div class="post-metadata">

**Author:** ![tchelyzt](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tchelyzt/32/111572_2.png) [@tchelyzt](https://forums.kodeco.com/u/tchelyzt)\
**Post date:** [November 17, 2022, 1:57pm UTC](https://forums.kodeco.com/t/why-does-this-not-work/179447/5 "2022-11-17T13:57:37Z")

</div>

Hello Jeden,  
yes thanks. Sorted it out  
Tchelyzt
