title: When does SwiftUI update its views?
body: <p>If you take a look at the <code>flagTapped</code> function, you will see that it modifies many <code>State</code> variables. With the use of breakpoints, I realized that <code>body</code> doesn't get updated the moment <code>round</code> gets modified, but only after the function has run.</p>
  <p>Could someone explain to me when does SwiftUI updates its views?</p>
  <pre><code>import SwiftUI
  
  struct ContentView: View {
      @State private var shouldShowWrongAnswer = false
      
      @State private var scoreTitle = &quot;&quot;
      @State private var score = 0
      @State private var round = 1
      
      // ----------------------
      
      @State private var countries = [&quot;Estonia&quot;, &quot;France&quot;, &quot;Germany&quot;, &quot;Ireland&quot;, &quot;Italy&quot;, &quot;Nigeria&quot;, &quot;Poland&quot;, &quot;Russia&quot;, &quot;Spain&quot;, &quot;UK&quot;, &quot;US&quot;]
      @State private var correctAnswer = Int.random(in: 0...2)
      
      // ----------------------
      
      var body: some View {
          ZStack {
              RadialGradient(stops: [
                  .init(color: Color(red: 0.1, green: 0.2, blue: 0.45), location: 0.3),
                  .init(color: Color(red: 0.76, green: 0.15, blue: 0.26), location: 0.3),
              ], center: .top, startRadius: 200, endRadius: 400)
                  .ignoresSafeArea()
              
              VStack {
                  VStack {
                      Spacer()
                      
                      Text(&quot;Guess the Flag&quot;)
                          .font(.largeTitle.weight(.bold))
                          .foregroundColor(.white)
                      
                      VStack(spacing: 15) {
                          Text(&quot;Tap the flag of&quot;)
                              .foregroundColor(.secondary)
                              .font(.subheadline.weight(.heavy))
                          
                          Text(countries[correctAnswer])
                              .font(.largeTitle.weight(.semibold))
                      }
                      .frame(maxWidth: .infinity)
                      .padding(.vertical, 20)
                      .background(.regularMaterial)
                      .clipShape(RoundedRectangle(cornerRadius: 20))
                      
                      Spacer()
                      Spacer()
                      
                      Text(&quot;Score: \(score)&quot;)
                          .foregroundColor(.white)
                          .font(.title.bold())
                      
                      Text(&quot;Round: \(round)/8&quot;)
                          .foregroundColor(.white)
                          .font(.caption)
                      
                      Spacer()
                  }
                  
                  ForEach(0..&lt;3) { number in
                      Button {
                          flagTapped(number)
                      } label: {
                          Image(countries[number])
                              .renderingMode(.original)
                              .clipShape(Capsule())
                              .shadow(radius: 5)
                      }
                  }
              }.padding()
          }
          .alert(scoreTitle, isPresented: $shouldShowWrongAnswer) {
              Button(&quot;Continue&quot;, action: askQuestion)
          } message: {
              Text(&quot;Your score is \(score)&quot;)
          }
      }
      
      func flagTapped(_ number: Int) {
          if round == 8 {
              shouldShowGameEnded = true
              return
          }
          
          round += 1
          
          if number == correctAnswer {
              score += 1
              askQuestion()
          } else {
              scoreTitle = &quot;Wrong! That’s the flag of \(countries[number])&quot;
              shouldShowWrongAnswer = true
          }
      }
      
      func askQuestion() {
          countries.shuffle()
          correctAnswer = Int.random(in: 0...2)
      }
  }
  </code></pre>