I am working on a project and I am sending a GET Request to an API URL in my project. (As follows)
Actually everything here works for me. But the problem is; When I first send the request, the "Status" value comes as -proceeding-. After a while it becomes -completed-. (Because I'm waiting for a render in the API)
My question is; How can I know when this status value changes from proceeding to completed? Because I want to do a different action when completed. Any idea about that ?
func fetchURL(url: String) {
let apiUrl = URL(string: url)!
let session = URLSession.shared
let task = session.dataTask(with: apiUrl) { (data, response, error) in
if let error = error {
print("DEBUG: - Error: \(error)")
return
}
guard let data = data else {
print("DEBUG: - No data received")
return
}
do {
let decoder = JSONDecoder()
let dustResponse = try decoder.decode(FetchData.self, from: data)
print("DEBUG: - Status: \(dustResponse.detail.status)")
print("DEBUG: - Message: \(dustResponse.detail.message)")
} catch {
print("DEBUG: - Error decoding JSON: \(error)")
}
}
task.resume()
}
As the process is made by the server of your API you can't really know when it has finished. I would suggest to see if you have a way to have some kind of web socket to be notified when the processing is finished. If you don't, you could request the status every x seconds to see if the processing is finished:
In this piece of code we:
fetchURLfunction by using the nativewithCheckedContinuationfunction of the Swift's standard library.finalResultis defined.Hope this will help you!