code Horizons
code Horizons

Reputation: 851

the app crashes when perform a segue

I'm pulling information from the internet using JSON and when I try to perform a segue after checking the data the segue crashes ! here is my code :

        passWord = password.text
         userName = username.text
        var link = "http://ksa-sms.com/SpecialapiK/getUser.php?return=json&username=\(userName!)&password=\(passWord!)"
        if let url = NSURL(string: link){
            var dataTask = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data: NSData!, response: NSURLResponse!, error : NSError!) -> Void in
                if error != nil{
                    println("Error : \(error.localizedDescription)")
                }else{
                    //                    println("Data : \(data)")

                    var jsonError = NSError?()
                    if let jsonData = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as? NSDictionary{
                        println(jsonData)
                        println(jsonData["Code"]!)
                         var code = jsonData["Code"]!.integerValue
                        var userdeta = jsonData["user"]!


                        if(code != 104){
                            self.errors.text = "Username or password is wrong !"

                        }else{
                            self.errors.text = "Welcome \(self.userName)"
                           self.userData = AppData(username: self.userName!, password: self.passWord!, phone: userdeta[6]!.integerValue, code: code!)
                            if(self.userData?.code! == 104){
                                self.performSegueWithIdentifier("logedIn", sender: sender)
                            }else{
                                println("Didn't work !")
                                println(self.userData?.code!)
                            }

                            println(self)


                        }

                    }

                    println(self.userData?.code!)

                }
            })

            dataTask.resume()


            }

when I try to perform the segue after dataTask.resume() , the app work fine (but there is no information !) , what I understand is that dataTask takes too much time and because of that it perform in the background while Xcode compile the rest of the code ! am I right ? and how can I fix this problem ?

Upvotes: 0

Views: 938

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50089

Perform the segue on the main thread.

    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        self.performSegueWithIdentifier("logedIn", sender: nil)
    }

Upvotes: 1

Related Questions