Please help me understand when SwiftUI subviews are re-initialized

See below playground code:

Code Block swift
import SwiftUI
import PlaygroundSupport
struct InnerView: View {
  @Binding var text: String
  @State private var startedChange = false
  var body: some View {
    Text("Hello").onAppear {
      if !self.startedChange {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
          self.text = "cat"
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
          self.text = "cheetah"
        }
        self.startedChange = true
      }
    }
  }
}
struct ContentView: View {
  @State private var animalName = "dog"
  var body: some View {
    VStack {
      InnerView(text: $animalName)
      Text(animalName)
    }
  }
}
PlaygroundPage.current.setLiveView(ContentView())


The bottom
Code Block swift
Text(animal.name)
alternates irregularly between "cat" and "cheetah," in an infinite loop of race conditions. I expected the text to change to "cat" after one second and then "cheetah" after another.

I would not have expected
Code Block swift
   @State private var startedChange = false
to be initialized more than once.

Where is my mental model wrong?

(Xcode 12.0)
Answered by OOPer in 635866022
Generally, it is not clear how Playground is managing SwiftUI view to show it in a live view.

Your code works as you expect in an actual app.
I guess your mental model is not wrong, just that Playground does something more than you expect.
Accepted Answer
Generally, it is not clear how Playground is managing SwiftUI view to show it in a live view.

Your code works as you expect in an actual app.
I guess your mental model is not wrong, just that Playground does something more than you expect.
Ah, that's fun...

Thanks for the help.
Please help me understand when SwiftUI subviews are re-initialized
 
 
Q