I have an API class in which I am creating a URL session data task that updates the value of a birdResult property in the class when the task completes from within a DispatchQueue.main.async block.
In Swift 6, the following error appears:
My class code looks like this:
class BirdsAPI: ObservableObject {
static let url = URL(string: "https://api.inaturalist.org/v1/observations?iconic_taxa=Aves&per_page=50&order=desc&order_by=created_at")
@Published var birdResults: [BirdResult] = []
func fetchObservations() {
URLSession.shared.dataTask(with: URLRequest(url: BirdsAPI.url!)) { data, response, error in
guard error == nil else { return }
let decoder = JSONDecoder()
do {
let body = try decoder.decode(Body.self, from: data!)
DispatchQueue.main.async {
self.birdResults = body.results
}
} catch let error {
print("error decoding data, (error)")
}
}.resume()
}
}
Why is this and how can I fix it?