25

I have a problem while trying to make a function that automatically create POST request, send them, get the response, and handle them then show UIAlertView to tell the user which problem it is.

Here is my code :

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();

The error code is the following :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

When my request return a bad response (the #400, #50, #100), the code works and show the UIAlertView but if the response is good, it gives me the error code as above.

2 Answers 2

34

You should call your UIAlertController on the main thread because you're dealing with the 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)
    {

    }
}
3
  • But the UI works when my server return #400, #50 or #100, so I guess it's coming from the success statement :/
    – Orionss
    Commented Sep 18, 2016 at 17:30
  • You're dealing with ui elements in another thread when you receive the response, but you are supposed to work with the ui only in the main thread. Commented Sep 18, 2016 at 17:32
  • 2
    Try to remove the While(!complete) and put all the code that you want to execute inside the completion of dismissViewControllerAnimated and use the dispatch_async(dispatch_get_main_queue() inside the completion to call that code. Commented Sep 18, 2016 at 17:51
22

For swift 3.x and 4.x

DispatchQueue.main.async(execute: {
// work Needs to be done
 })
2
  • 1
    thanks it works for me but can someone explain a bit more why does it solve the problem?
    – Cinn
    Commented Apr 16, 2018 at 17:28
  • 3
    You're using a back thread that call the request to the ws you're calling, and iOS only allows you to change the UI components on the main thread, so calling this: DispatchQueue.main.async, you're returning to the main thread (UI) to change/handle UI changes Commented Aug 17, 2018 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.