This is a companion discussion topic for the original entry at https://www.kodeco.com/32059632-modern-concurrency-beyond-the-basics/lessons/8
This is a companion discussion topic for the original entry at https://www.kodeco.com/32059632-modern-concurrency-beyond-the-basics/lessons/8
Hi, running the project from the starting code return SWIFT TASK CONTINUATION MISUSE: shareLocation() leaked its continuation! instead of lat,lng as in your Debug area.
Update: same error at the end of tutorial, deleting app and giving access for location didnโt solve the issue.
Hi Eytan! The note says
At the time of recording, Xcode 14 flags a runtime error in the location delegate continuation. If this happens to you, use Xcode 13 for the rest of Part 1.
This long after recording, itโs probably not easy to get back to Xcode 13. The Modern Concurrency book was updated in Feb 2023, and the code in Chapter 5 has a different location delegate:
class ChatLocationDelegate: NSObject, CLLocationManagerDelegate {
typealias LocationContinuation = CheckedContinuation<CLLocation, Error>
private var continuation: LocationContinuation?
init(manager: CLLocationManager, continuation: LocationContinuation) {
self.continuation = continuation
super.init()
manager.delegate = self
manager.requestWhenInUseAuthorization()
}
...
and in BlabberModel:
let location: CLLocation = try await
withCheckedThrowingContinuation { [weak self] continuation in
self?.delegate = ChatLocationDelegate(manager: manager, continuation: continuation)
if manager.authorizationStatus == .authorizedWhenInUse {
manager.startUpdatingLocation()
}
}
Even this needs further update, as now you need explicit use of self inside the closure.
Hi, itโs November 2024 and this note
At the time of recording, Xcode 14 flags a runtime error in the location delegate continuation. If this happens to you, use Xcode 13 for the rest of Part 1.
still remains the same.
I had only found out about the working code, regardless of the explicit self warning, after digging into the forum. What stops you from updating the course note?
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)
}
///...
}