How to tell the main thread that a URLSessionDataTask has finished
If I understood correctly you might try this solution:
spinner.startAnimation(self)btnOk.isEnabled = falsebtnCancel.isEnabled = falseattemptPost { (success) in DispatchQueue.main.async { spinner.stopAnimation(self) btnOk.isEnabled = true btnCancel.isEnabled = true } // UI wise, eventually you can do something with 'success'}func attemptPost(_ completion:@escaping (Bool)->()) let url = Constants.SERVICE_URL + "account/post" let body: [String : Any] = ["firstName": txtFirstName.stringValue, "lastName": txtLastName.stringValue, "email": txtEmail.stringValue, "password": txtPassword.stringValue]; let req = Request.create(urlExtension: url, httpVerb: Constants.HTTP_POST, jsonBody: body) let task = URLSession.shared.dataTask(with: req) { data, response, err in guard let data = data, err == nil else { completion(false) return } if let resp = try? JSONSerialization.jsonObject(with: data) { completion(true) } } task.resume()}
so the idea is executing from attemptPost
a block which will run asynchronously into the main thread your UI stuff