URLSession Fetch With Loading State
Spot the bug in a URLSession REST fetch that manages loading and error states.
Codeswift
func fetchUsers() {
isLoading = true
let url = URL(string: "https://api.example.com/users")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
self.isLoading = false
if let error = error {
self.errorMessage = error.localizedDescription
return
}
guard let data = data else { return }
self.users = try? JSONDecoder().decode([User].self, from: data)
}
}What is the bug in this URLSession fetch?