2016-09-18 18 views
11

Mam problem podczas próby utworzenia funkcji, która automatycznie tworzy żądanie POST, wysyła je, otrzymuje odpowiedź i obsługuje je, a następnie wyświetla UIAlertView, aby poinformować użytkownika o tym problemie.Funkcja UIKeyboardTaskQueue może być wywoływana tylko z głównego wątku

Oto mój kod:

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

Kod błędu jest następujący:

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

Kiedy moja prośba powrót złą odpowiedź (the # 400, # 50, # 100), Kod działa i pokazuje UIAlertView, ale jeśli odpowiedź jest dobra, daje mi kod błędu jak powyżej.

Odpowiedz

17

Powinieneś zadzwonić do swojego sterownika UIAlertController w głównym wątku, ponieważ masz do czynienia z interfejsem użytkownika.

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) 
{ 

} 
+0

Ale UI działa, gdy mój serwer powrót # 400, # 50 lub # 100, więc myślę, że to przychodzi z rachunku sukcesu:/Jesteś – Orionss

+0

zajmowanie się elementami interfejsu użytkownika w innym wątku po otrzymaniu odpowiedzi, ale użytkownik powinien pracować z interfejsem tylko w głównym wątku. –

+0

Próbowałem tego (wysłałem nowy kod), ale to nie działa ... – Orionss

12

Dla szybkiej 3.x

DispatchQueue.main.async(execute: { 
// work Needs to be done 
}) 
Powiązane problemy