elkin sepulveda elkin sepulveda - 3 months ago 7
Swift Question

swift how to use dispatch_async

I want to send some data to the server but when i try , i get this error

enter image description here

I think is a problem with dispatch maybe i am not using it correctly but i have no experience with xcode or swift, so i dont know how to understand well the log error window, any help is welcome , thaks for ur time

this is my code

func updateAndDismiss6() {
SharingManager.sharedInstance.FinalDiag = self.Diag.text!
SharingManager.sharedInstance.TreatmentDays = self.Trata.text!
SharingManager.sharedInstance.SystematicTreat = self.TrataSu.text!
SharingManager.sharedInstance.LidoPatch = self.ParcheLido.text!
}

@IBAction func IrResumen(sender: AnyObject) {

updateAndDismiss6()

let prefs = NSUserDefaults.standardUserDefaults()
let userid = prefs.stringForKey("userid")! as String

let Inicial = SharingManager.sharedInstance.Initials
let Edad = SharingManager.sharedInstance.Age
let Resultados = SharingManager.sharedInstance.Result
let Diagnostico = SharingManager.sharedInstance.FinalDiag
let Ttratamiento = SharingManager.sharedInstance.TreatmentDays
let TsUsado = SharingManager.sharedInstance.SystematicTreat
let Plidocaina = SharingManager.sharedInstance.LidoPatch




if ( Inicial == "" || Edad == "" || Resultados == "" || Diagnostico == "" || Ttratamiento == "" || TsUsado == "" || Plidocaina == "" || userid == "") {

let alertView:UIAlertView = UIAlertView()
alertView.title = "No data"
alertView.message = "error de datos"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
else {



let url_servicio = "http://arasoftltda.com/grunenthal/app/muestra.php"

let request = NSMutableURLRequest(URL: NSURL(string: url_servicio)!)
request.HTTPMethod = "POST"
//let postString = "nombre=pruebafromios&email=pruebafromios@pruebafromios.com&registro=registro&esp=esp&pais=Colombia&ciudad=Cogua&contrasena=123"
let postString = "iniciales=\(Inicial)&edad=\(Edad)&resultado=\(Resultados)&diagnostico=\(Diagnostico)&dias=\(Ttratamiento)&tratamiento=\(TsUsado)&parche=\(Plidocaina)&id=\(userid)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error -> Void in
if (error != nil){
print(error!.localizedDescription)
} else{
let nsdata:NSData = NSData(data: data!)

do{
let json = try NSJSONSerialization.JSONObjectWithData(nsdata, options: NSJSONReadingOptions.MutableContainers)
let id = json["id"] as! String



if (id != ""){
print (id)

dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
self.performSegueWithIdentifier("Resumen", sender: self)}
}

}

catch{
print("Error del JSON")
let alertView:UIAlertView = UIAlertView()
alertView.title = "Fallo al enviar datos!"
alertView.message = "Error de conexion con el servidor"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
}

}
task.resume()

}

}

Rob Rob
Answer

In addition to dispatching performSegueWithIdentifier to the main queue, you have to dispatch your UIAlertView/UIAlertController to the main queue, too. If you look at your stack trace, there are UIAlertView references in there, so that's what's causing the problem here.

Comments