I have the following code
APICaller.sharedInstance.performCallToEndpoint("/user/session", using: "GET", body: nil) { (error, serverError, response) -> () in
if let response = response {
self.setUserToken(response);
completion(true)
} else {
completion(false)
}
}
Where I’m calling setUserToken I get the error Call to main actor-isolated instance method 'setUserToken' in a synchronous nonisolated context
.
Here’s the code for performCallToEndpoint
APICaller {
func performCallToEndpoint(_ endpoint:String, using method:String, body: [String: Any]?, completion: @escaping (_ error:Error?, _ serverError: String?, _ response: NSDictionary?) -> Void) {
let request = createURLRequest(toEndpoint: endpoint, using:method, body: body)
URLSession.shared.dataTask(with: request) { data, response, error in
guard error == nil else {
print("Error: error calling POST")
completion(error, nil, nil)
return
}
guard let data = data else {
print("Error: Did not receive data")
completion(nil, nil, nil)
return
}
guard let response = response as? HTTPURLResponse, (200 ..< 299) ~= response.statusCode else {
print("Error: HTTP request failed")
completion(nil, nil, nil)
return
}
do {
guard let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary else {
return
}
let responseError = jsonObject["error"] as? String
if responseError?.count != nil {
completion(nil, responseError, nil)
} else {
if (jsonObject["result"] == nil) {
completion(nil, nil, jsonObject as! NSDictionary)
} else {
completion(nil, nil, jsonObject["result"] as! NSDictionary)
}
}
} catch {
print("Error: Trying to convert JSON data to string")
return
}
}.resume()
}
}
What am I doing wrong? I’m not able to build my swift project after upgrading to the latest version of Xcode.