2016-09-18 11 views
11

से कहा जा सकता है कि मैं एक समारोह है कि स्वचालित रूप से पोस्ट अनुरोध बनाने के लिए, उन्हें भेजने के लिए, प्रतिक्रिया मिल, और उन्हें तो संभाल UIAlertView दिखाने उपयोगकर्ता जो समस्या यह है बताने के लिए बनाने की कोशिश करते हुए एक समस्या है।UIKeyboardTaskQueue केवल मुख्य थ्रेड

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.' 

जब मेरे अनुरोध एक बुरी प्रतिक्रिया (# 400, # 50, # 100) लौटने के लिए, कोड काम करता है और UIAlertView दिखाता है लेकिन अगर प्रतिक्रिया अच्छी है, तो यह मुझे ऊपर के रूप में त्रुटि कोड देता है।

उत्तर

17

आप मुख्य थ्रेड पर अपने UIAlertController बुलाना चाहिए क्योंकि आप ui साथ काम कर रहे।

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

के लिए लेकिन यूआई काम करता है जब अपने सर्वर वापसी # 400, # 50 या # 100, इसलिए मुझे लगता है कि यह सफलता बयान से आ रहा है:/ – Orionss

+0

तुम जब आप प्रतिक्रिया प्राप्त करते हैं तो किसी अन्य धागे में ui तत्वों से निपटना, लेकिन आपको केवल मुख्य धागे में यूई के साथ काम करना चाहिए। –

+0

मैं इस कोशिश की (मैं नए कोड अपलोड किया गया) लेकिन यह काम करने के लिए ... – Orionss

12

तेज 3.x

DispatchQueue.main.async(execute: { 
// work Needs to be done 
}) 
संबंधित मुद्दे