If they had used .task
they could have removed the class LocationsHandler: ObservableObject
and simply done:
struct ContentView: View {
@State var lastLocation: CLLocation?
var body: some View {
VStack {
...
}
.task {
let updates = CLLocationUpdate.liveUpdates()
for try await update in updates {
if let loc = update.location {
self.lastLocation = loc
}
}
}
And saved themselves about 20 or so lines of code. .task
was added in the year before so it isn't the case that it wasn't available to the CoreLocation team yet. To wrap async/await in a Combine's ObservableObject
is very strange. They could have also used @AppStorage
instead of UserDefaults
and saved another few lines. To be honest this is some of the strangest SwiftUI code I've seen.