title: Swift task and await/async queue after `await` execution?
body: <p>I am trying to understand what the rule is when calling <code>Task { ... }</code> and, in that task, calling <code>await</code> in terms of threads.</p>
  <p>This example works:</p>
  <pre><code>struct TaskTestView: View {
      
      let url = URL(string: &quot;https://www.google.com&quot;)!
      @State private var message = &quot;Loading...&quot;
  
      var body: some View {
          Text(message)
              .task {
                  /// on MAIN THREAD
                  do {
                      var receivedLines = [String]()
                      for try await line in url.lines {
                   /// on MAIN THREAD
                          receivedLines.append(line)
                          message = &quot;Received \(receivedLines.count) lines&quot;
                      }
                  } catch {
                      message = &quot;Failed to load&quot;
                  }
              }
      }
  }
  </code></pre>
  <p>This does not:</p>
  <pre><code>struct TaskTestView: View {
  
      @StateObject var model = TaskTestViewModel()
      
      var body: some View {
          Text(model.message)
              .task {
                  /// - here it is on main thread
                  await model.refresh()
                  /// - here it is NOT on main thread
                  print(&quot;after refresh: on main?&quot;)
              }
      }
  }
  
  class TaskTestViewModel:ObservableObject {
      
      let url = URL(string: &quot;https://www.google.com&quot;)!
      @Published private(set) var message = &quot;Loading...&quot;
      
      func refresh() async {
          
          do {
              var receivedLines = [String]() // on main thread
              for try await line in url.lines {
                  receivedLines.append(line) // NOT on main thread
                  message = &quot;Received \(receivedLines.count) lines&quot;
              }
          } catch {
              message = &quot;Failed to load&quot;
          }
          
      }
      
  }
  </code></pre>
  <ol>
  <li>Why does the code run on the main thread after line <code>for try await line in url.lines {</code> in the first example?</li>
  <li>Why does the code NOT run on the main thread after that same line but in the second example?</li>
  </ol>
  <p>How would I know the answer to those questions without running the code and putting a breakpoint to inspect the thread I am on?</p>
  <p>Obviously the main issue here is that I want to make sure I update @State variables on main so the view works properly, but, not having this concept clear makes it hard to design proper patterns.</p>