swift 4 error : '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread
swift 4 error : '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread
该应用程序在互联网连接处于活动状态时运行良好。但是,我尝试关闭互联网连接并尝试使用端点。
我遇到了这个错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
几点:
a) 首先,我不清楚我应该在哪个地方使用 async - 我在 switch 语句中针对两种不同的情况将它放在了两个地方。
b) 第二,我用 error.localizedDescription
处理错误了吗?我想做的是找到一种方法来处理网络关闭时的 1009 错误。
如果我要求的答案过长,请指导我找到可以阅读的资源。
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error{
self.errorMessage += "Data Gathering Error: " + error.localizedDescription + "\n"
completion(self.errorMessage, nil)
return
} else if let data = data, let response = response as? HTTPURLResponse{
print(response.statusCode)
if response.statusCode == 200 {
do {
switch relativeURL{
case .requestOTP:
print("------------")
print(String(data: data, encoding: .utf8)!)
print("------------")
let responseInfo = try JSONDecoder().decode(loginResponse.self, from: data)
print(responseInfo.success)
print(responseInfo.message)
DispatchQueue.main.async {
let dataReceived = responseData(loginResponse: .init(success: responseInfo.success, error: .init(message:responseInfo.error?.message), message: responseInfo.message), decodeOTPResponse: nil,quotaResponse:nil,UserAddResponse:nil)
print(dataReceived)
completion(nil,dataReceived)
}
case .loginWithOTP:
let responseInfo = try JSONDecoder().decode(decodeOTPResponse.self, from: data)
let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: .init(success: responseInfo.success, token: responseInfo.token, error: .init(message:responseInfo.error?.message), totp_secret: responseInfo.totp_secret),quotaResponse:nil,UserAddResponse:nil)
print(dataReceived)
DispatchQueue.main.async {
completion(nil,dataReceived)
}
case .addUser:
let responseInfo = try JSONDecoder().decode(UserAddResponse.self, from: data)
print(responseInfo)
let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: nil, quotaResponse: nil, UserAddResponse:.init(success: responseInfo.success, error:.init(message:responseInfo.error?.message), message: responseInfo.message))
DispatchQueue.main.async {
completion(nil,dataReceived)
}
default:
completion("Wrong request call",nil)
return
}
} catch let jsError{
print("Error serialising JSON", jsError)
completion("Error Serialising JSON",nil)
return
}
} else if response.statusCode > 401 && response.statusCode < 500{
print("Unauthorized to perform action")
} else if response.statusCode == 500{
print("endpoint not found")
}
}
}
task.resume()
尝试覆盖主队列中的所有 completion()(不确定是否可行,但试试这个。)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error{
self.errorMessage += "Data Gathering Error: " + error.localizedDescription + "\n"
DispatchQueue.main.async {
completion(self.errorMessage, nil)
}
return
} else if let data = data, let response = response as? HTTPURLResponse{
print(response.statusCode)
if response.statusCode == 200 {
do {
switch relativeURL{
case .requestOTP:
print("------------")
print(String(data: data, encoding: .utf8)!)
print("------------")
let responseInfo = try JSONDecoder().decode(loginResponse.self, from: data)
print(responseInfo.success)
print(responseInfo.message)
DispatchQueue.main.async {
let dataReceived = responseData(loginResponse: .init(success: responseInfo.success, error: .init(message:responseInfo.error?.message), message: responseInfo.message), decodeOTPResponse: nil,quotaResponse:nil,UserAddResponse:nil)
print(dataReceived)
completion(nil,dataReceived)
}
case .loginWithOTP:
let responseInfo = try JSONDecoder().decode(decodeOTPResponse.self, from: data)
let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: .init(success: responseInfo.success, token: responseInfo.token, error: .init(message:responseInfo.error?.message), totp_secret: responseInfo.totp_secret),quotaResponse:nil,UserAddResponse:nil)
print(dataReceived)
DispatchQueue.main.async {
completion(nil,dataReceived)
}
case .addUser:
let responseInfo = try JSONDecoder().decode(UserAddResponse.self, from: data)
print(responseInfo)
let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: nil, quotaResponse: nil, UserAddResponse:.init(success: responseInfo.success, error:.init(message:responseInfo.error?.message), message: responseInfo.message))
DispatchQueue.main.async {
completion(nil,dataReceived)
}
default:
DispatchQueue.main.async {
completion("Wrong request call",nil)
}
return
}
} catch let jsError{
print("Error serialising JSON", jsError)
DispatchQueue.main.async {
completion("Error Serialising JSON",nil)
}
return
}
} else if response.statusCode > 401 && response.statusCode < 500{
print("Unauthorized to perform action")
} else if response.statusCode == 500{
print("endpoint not found")
}
}
}
task.resume()
我也被这个错误困扰了两天。直到我将我的代码放入
DispatchQueue.main.asyn
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
DispatchQueue.main.async {
let userId = parseJSON["firstName"] as? String
print("User id: \(String(describing: userId!))")
if (userId?.isEmpty)!
{
// Display an Alert dialog with a friendly error message
self.showAlert(title: "Request Error", message: "Could not successfully perform this request. Please try again later")
return
} else {
self.showAlert(title: "SignIn Successfully", message: "Successfully Registered a New Account. Please proceed to Sign in")
}
}
}
else {
//Display an Alert dialog with a friendly error message
// self.showAlert(title: "Request Error", message: "Could not successfully perform this request. Please try again later")
}
} catch {
// self.removeActivityIndicator(activityIndicator: myActivityIndicator)
// Display an Alert dialog with a friendly error message
self.showAlert(title: "Request Error", message: "Could not successfully perform this request. Please try again later")
print(error)
}
}
task.resume()
}
该应用程序在互联网连接处于活动状态时运行良好。但是,我尝试关闭互联网连接并尝试使用端点。
我遇到了这个错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
几点:
a) 首先,我不清楚我应该在哪个地方使用 async - 我在 switch 语句中针对两种不同的情况将它放在了两个地方。
b) 第二,我用 error.localizedDescription
处理错误了吗?我想做的是找到一种方法来处理网络关闭时的 1009 错误。
如果我要求的答案过长,请指导我找到可以阅读的资源。
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error{
self.errorMessage += "Data Gathering Error: " + error.localizedDescription + "\n"
completion(self.errorMessage, nil)
return
} else if let data = data, let response = response as? HTTPURLResponse{
print(response.statusCode)
if response.statusCode == 200 {
do {
switch relativeURL{
case .requestOTP:
print("------------")
print(String(data: data, encoding: .utf8)!)
print("------------")
let responseInfo = try JSONDecoder().decode(loginResponse.self, from: data)
print(responseInfo.success)
print(responseInfo.message)
DispatchQueue.main.async {
let dataReceived = responseData(loginResponse: .init(success: responseInfo.success, error: .init(message:responseInfo.error?.message), message: responseInfo.message), decodeOTPResponse: nil,quotaResponse:nil,UserAddResponse:nil)
print(dataReceived)
completion(nil,dataReceived)
}
case .loginWithOTP:
let responseInfo = try JSONDecoder().decode(decodeOTPResponse.self, from: data)
let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: .init(success: responseInfo.success, token: responseInfo.token, error: .init(message:responseInfo.error?.message), totp_secret: responseInfo.totp_secret),quotaResponse:nil,UserAddResponse:nil)
print(dataReceived)
DispatchQueue.main.async {
completion(nil,dataReceived)
}
case .addUser:
let responseInfo = try JSONDecoder().decode(UserAddResponse.self, from: data)
print(responseInfo)
let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: nil, quotaResponse: nil, UserAddResponse:.init(success: responseInfo.success, error:.init(message:responseInfo.error?.message), message: responseInfo.message))
DispatchQueue.main.async {
completion(nil,dataReceived)
}
default:
completion("Wrong request call",nil)
return
}
} catch let jsError{
print("Error serialising JSON", jsError)
completion("Error Serialising JSON",nil)
return
}
} else if response.statusCode > 401 && response.statusCode < 500{
print("Unauthorized to perform action")
} else if response.statusCode == 500{
print("endpoint not found")
}
}
}
task.resume()
尝试覆盖主队列中的所有 completion()(不确定是否可行,但试试这个。)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error{
self.errorMessage += "Data Gathering Error: " + error.localizedDescription + "\n"
DispatchQueue.main.async {
completion(self.errorMessage, nil)
}
return
} else if let data = data, let response = response as? HTTPURLResponse{
print(response.statusCode)
if response.statusCode == 200 {
do {
switch relativeURL{
case .requestOTP:
print("------------")
print(String(data: data, encoding: .utf8)!)
print("------------")
let responseInfo = try JSONDecoder().decode(loginResponse.self, from: data)
print(responseInfo.success)
print(responseInfo.message)
DispatchQueue.main.async {
let dataReceived = responseData(loginResponse: .init(success: responseInfo.success, error: .init(message:responseInfo.error?.message), message: responseInfo.message), decodeOTPResponse: nil,quotaResponse:nil,UserAddResponse:nil)
print(dataReceived)
completion(nil,dataReceived)
}
case .loginWithOTP:
let responseInfo = try JSONDecoder().decode(decodeOTPResponse.self, from: data)
let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: .init(success: responseInfo.success, token: responseInfo.token, error: .init(message:responseInfo.error?.message), totp_secret: responseInfo.totp_secret),quotaResponse:nil,UserAddResponse:nil)
print(dataReceived)
DispatchQueue.main.async {
completion(nil,dataReceived)
}
case .addUser:
let responseInfo = try JSONDecoder().decode(UserAddResponse.self, from: data)
print(responseInfo)
let dataReceived = responseData(loginResponse: nil, decodeOTPResponse: nil, quotaResponse: nil, UserAddResponse:.init(success: responseInfo.success, error:.init(message:responseInfo.error?.message), message: responseInfo.message))
DispatchQueue.main.async {
completion(nil,dataReceived)
}
default:
DispatchQueue.main.async {
completion("Wrong request call",nil)
}
return
}
} catch let jsError{
print("Error serialising JSON", jsError)
DispatchQueue.main.async {
completion("Error Serialising JSON",nil)
}
return
}
} else if response.statusCode > 401 && response.statusCode < 500{
print("Unauthorized to perform action")
} else if response.statusCode == 500{
print("endpoint not found")
}
}
}
task.resume()
我也被这个错误困扰了两天。直到我将我的代码放入
DispatchQueue.main.asyn
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
DispatchQueue.main.async {
let userId = parseJSON["firstName"] as? String
print("User id: \(String(describing: userId!))")
if (userId?.isEmpty)!
{
// Display an Alert dialog with a friendly error message
self.showAlert(title: "Request Error", message: "Could not successfully perform this request. Please try again later")
return
} else {
self.showAlert(title: "SignIn Successfully", message: "Successfully Registered a New Account. Please proceed to Sign in")
}
}
}
else {
//Display an Alert dialog with a friendly error message
// self.showAlert(title: "Request Error", message: "Could not successfully perform this request. Please try again later")
}
} catch {
// self.removeActivityIndicator(activityIndicator: myActivityIndicator)
// Display an Alert dialog with a friendly error message
self.showAlert(title: "Request Error", message: "Could not successfully perform this request. Please try again later")
print(error)
}
}
task.resume()
}