I have this simple code to perform a network request:
class Downloader {
var isCompleted = false
func start() {
URLSession.shared.dataTask(with: url) { data, response, error in
isCompleted = true
}
}
}
I want to check isCompleted
in other code to determine if the request is completed or not.
The problem is that I seem to have created data race this way, because the flag is set in a background queue, and it might be checked in any queue.
Is there any way to make isCompleted
thread safe using GCD?