2

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 don't 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 protected]&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()

    }

    }
3
  • 1
    Unrelated, but I would advise against using unowned self in this context. If the view controller really could be dismissed, you'd use weak self. But unowned can leave you with a dangling reference. Also, I'd advise against using UIAlertView. Nowadays you'd use UIAlertController. Commented Aug 11, 2016 at 0:35
  • In terms of why your JSON request is failing, I'd suggest you print the postString and make sure (a) that none of those variables were optionals that needed to be unwrapped; and (b) that there weren't any unreserved characters (e.g. punctuation, spaces, etc.). When you compose a x-www-form-urlencoded request, you generally would percent escape the values, just to be safe. Commented Aug 11, 2016 at 0:39
  • thx for ur advise pal Commented Aug 11, 2016 at 0:45

2 Answers 2

2

Your error message is telling you that it tried to update the UI (in this case, the auto layout constraints) from a background thread. The presence of numerous UIAlertView and UIAlertController references in the stack trace is telling you that the source of the problem is the UIAlertView.

Bottom line, all UI updates must be dispatched to the main queue. So, in addition to dispatching performSegueWithIdentifier to the main queue, you must dispatch your UIAlertView/UIAlertController to the main queue, too.

Sign up to request clarification or add additional context in comments.

Comments

1
func showError() {
        dispatch_async(dispatch_get_main_queue()) { [unowned self] in
            let ac = UIAlertController(title: "Loading error", message: "There was a problem loading the feed; please check your connection and try again.", preferredStyle: .Alert)
            ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
            self.presentViewController(ac, animated: true, completion: nil)
        }
    }

You are doing it while performing segue. Try the same thing while showing alerts!!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.