Modern Concurrency: Beyond the Basics, Episode 8: Wrapping Callback With Continuation | Kodeco

What about running all location operations in the same thread? As @srinath2763 pointed out in another post Modern Concurrency: Beyond the Basics, Episode 7: Wrapping Delegate With Continuation | Kodeco - #2 by srinath2763 .

It seems it can be done with a Swift 6 concurrency style, by marking the ChatLocationDelegate class, and its CLLocationManagerDelegate implementation in the main thread.

@MainActor
class ChatLocationDelegate: NSObject, @MainActor CLLocationManagerDelegate {
/// ...
}

As well as wrapping the delegate initialisation in a main thread task.

/// The app model that communicates with the server.
class BlabberModel: ObservableObject {

  /// Shares the current user's address in chat.
  func shareLocation() async throws {
    let location: CLLocation =
    try await withCheckedThrowingContinuation { [weak self] continuation in
      Task { @MainActor in
        self?.delegate = ChatLocationDelegate(continuation: continuation)
      }
    }
    print(location.description)
  }
 
  ///...
}