UIKeyboardTaskQueue 只能从主线程调用
UIKeyboardTaskQueue may only be called from the main thread
我在尝试制作一个自动创建 POST 请求、发送它们、获取响应并处理它们然后显示 UIAlertView 以告诉用户这是哪个问题的函数时遇到了问题。
这是我的代码:
let task = session.dataTaskWithRequest(request, completionHandler:{ (data, response, error) -> Void in
dispatch_async(dispatch_get_main_queue(),{
var alert = UIAlertController(title: "Chargement", message: "Envoi des informations...", preferredStyle: UIAlertControllerStyle.Alert)
viewController.presentViewController(alert, animated: true, completion: nil)
answer = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print(answer)
var complete = false
alert.dismissViewControllerAnimated(true, completion: { () -> Void in
complete = true
})
while(!complete)
{
}
var textmsg: String
if(answer == "#400")
{
textmsg = "Il manque une information !"
}
else if(answer == "#50")
{
textmsg = "Le compte fourni ne correspond pas."
}
else if(answer == "#100")
{
textmsg = "Impossible d'identifier l'application."
}
else if(answer == "#1")
{
textmsg = "Transfert terminé avec succès !"
}
else {
textmsg = "Echec du transfert."
}
let alertComplete = UIAlertController(title: "Chargement", message: textmsg, preferredStyle: UIAlertControllerStyle.Alert)
alertComplete.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
viewController.presentViewController(alertComplete, animated: true, completion: nil)
})
})
task.resume();
错误代码如下:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
当我的请求 return 收到错误的响应(#400、#50、#100)时,代码可以正常工作并显示 UIAlertView,但如果响应良好,它会给我上面的错误代码.
您应该在主线程上调用 UIAlertController,因为您正在处理 ui。
Swift 2.X
dispatch_async(dispatch_get_main_queue(),{
var alert = UIAlertController(title: "Chargement", message: "Envoi des informations...", preferredStyle: UIAlertControllerStyle.Alert)
viewController.presentViewController(alert, animated: true, completion: nil)
answer = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print(answer)
var complete = false
alert.dismissViewControllerAnimated(true, completion: { () -> Void in
complete = true
})
while(!complete)
{
}
}
Swift 4.2
DispatchQueue.main.async {
var alert = UIAlertController(title: "Chargement", message: "Envoi des informations...", preferredStyle: UIAlertControllerStyle.Alert)
viewController.presentViewController(alert, animated: true, completion: nil)
let answer = String(data: data!, encoding: .utf8)!
print(answer)
var complete = false
alert.dismissViewControllerAnimated(true, completion: { () -> Void in
complete = true
})
while(!complete)
{
}
}
对于swift3.x和4.x
DispatchQueue.main.async(execute: {
// work Needs to be done
})
我在尝试制作一个自动创建 POST 请求、发送它们、获取响应并处理它们然后显示 UIAlertView 以告诉用户这是哪个问题的函数时遇到了问题。
这是我的代码:
let task = session.dataTaskWithRequest(request, completionHandler:{ (data, response, error) -> Void in
dispatch_async(dispatch_get_main_queue(),{
var alert = UIAlertController(title: "Chargement", message: "Envoi des informations...", preferredStyle: UIAlertControllerStyle.Alert)
viewController.presentViewController(alert, animated: true, completion: nil)
answer = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print(answer)
var complete = false
alert.dismissViewControllerAnimated(true, completion: { () -> Void in
complete = true
})
while(!complete)
{
}
var textmsg: String
if(answer == "#400")
{
textmsg = "Il manque une information !"
}
else if(answer == "#50")
{
textmsg = "Le compte fourni ne correspond pas."
}
else if(answer == "#100")
{
textmsg = "Impossible d'identifier l'application."
}
else if(answer == "#1")
{
textmsg = "Transfert terminé avec succès !"
}
else {
textmsg = "Echec du transfert."
}
let alertComplete = UIAlertController(title: "Chargement", message: textmsg, preferredStyle: UIAlertControllerStyle.Alert)
alertComplete.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
viewController.presentViewController(alertComplete, animated: true, completion: nil)
})
})
task.resume();
错误代码如下:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
当我的请求 return 收到错误的响应(#400、#50、#100)时,代码可以正常工作并显示 UIAlertView,但如果响应良好,它会给我上面的错误代码.
您应该在主线程上调用 UIAlertController,因为您正在处理 ui。
Swift 2.X
dispatch_async(dispatch_get_main_queue(),{
var alert = UIAlertController(title: "Chargement", message: "Envoi des informations...", preferredStyle: UIAlertControllerStyle.Alert)
viewController.presentViewController(alert, animated: true, completion: nil)
answer = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print(answer)
var complete = false
alert.dismissViewControllerAnimated(true, completion: { () -> Void in
complete = true
})
while(!complete)
{
}
}
Swift 4.2
DispatchQueue.main.async {
var alert = UIAlertController(title: "Chargement", message: "Envoi des informations...", preferredStyle: UIAlertControllerStyle.Alert)
viewController.presentViewController(alert, animated: true, completion: nil)
let answer = String(data: data!, encoding: .utf8)!
print(answer)
var complete = false
alert.dismissViewControllerAnimated(true, completion: { () -> Void in
complete = true
})
while(!complete)
{
}
}
对于swift3.x和4.x
DispatchQueue.main.async(execute: {
// work Needs to be done
})