2

I have a function for login, when the user is valid i dont have a problem, but when the user is isvalid I have this error

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

This is my code.

@IBAction func login(sender: AnyObject) {
    var valid: Bool = false
    //activity.startAnimating()
    self.viewUtils.showActivityIndicator(self.view)
    username =  userField.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    password = passwordTextField.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

        if (self.username.isEmpty || self.password.isEmpty){
            self.showAlert("Inserte usuario password")
        }

        else{                   
            var user = UserFunction()
            user.userValid(self.username,password: self.password){ success in

                if success == true{
                    //self.activity.stopAnimating()
                    self.performSegueWithIdentifier("showTable", sender: self )
                }else{
                    println("me quedo aqui")
                    //self.activity.stopAnimating()
                    self.showAlert("usuario incorrecto")//ver porque no entra a usuario incorrecto

                }
           }
    }
}

func userValid(username :String, password : String, completionHandler:(Bool)->()){
    var valid: Bool!
    var resultados : Array<JSON> = []
    userBase64 = self.encodeToBase64(username)
    passBase64 = self.encodeToBase64(password)

    var api = ChannelsFunction()

    api.loadVideos("https://api.cxntv.com/api/v1/videos/?type=canales&page_size=100&ordering=-id"){results in


        if (results != nil){
            errorMessage = ""
            println("devuelvo true")
            api.saveLiveChannels(results!)
            self.saveUser(userBase64, passbase64: passBase64, username: username, password: password)
            completionHandler(true)
        }else{
            println("devuelvo false")
            completionHandler(false)
    }
    }
}
KuKeC
  • 4,392
  • 5
  • 31
  • 60
BarneyL. BarStin
  • 333
  • 1
  • 7
  • 20

1 Answers1

2

If you're interacting with some API that happens to spawn a thread and run your code in the background, you'll be running on the main thread.

Present your alert in main thread this way:

dispatch_async(dispatch_get_main_queue()) {
    self.showAlert("usuario incorrecto")
}

or if you are updating any other UI then you have to do that in your main thread as shown in above code.

For more info read this post: GCD to perform task in main thread

Community
  • 1
  • 1
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165