M
M
mechanicnikodimich2015-08-03 17:12:14
Apple Xcode
mechanicnikodimich, 2015-08-03 17:12:14

How to fix error when checking value in NSURLSession in SWIFT?

Please tell me, when processing json in NSURLSession in a certain state, you need to display an error.
I do it through AlertController, but it constantly pours in.
Here is Alert itself

func dispayMyAlertMassage (userMessage : String)
    {
        var myAlert = UIAlertController(title: "Ошибка", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
        
        let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
        
        myAlert.addAction(okAction)
        
        self.presentViewController(myAlert,animated: true, completion : nil)
    }

@IBAction func LoginButton(sender: AnyObject){
        
     
        // сохраняем имя пользователя
        let userName = nameText.text
        // сохраняем пароль пользователя
        let userPassword = PasswordText.text
        
        // ссылка на класс
        let myUrl = NSURL ( string: "http://localhost:8080/test1d/directURL?name="+userName+"&pass="+userPassword);        
        let request = NSMutableURLRequest(URL: myUrl);
        request.HTTPMethod = "GET"; 
        if userName.isEmpty & userPassword.isEmpty
        {
            self.dispayMyAlertMassage ("Введите данные пользователя!");
        }
        
        else
        
        {
            
            let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
                {
                    data,response,error in
                    
                    if error != nil {
                        println("error=\(error)")
                    }
                    
                    
                    var err: NSError?
                    var json = NSJSONSerialization.JSONObjectWithData (data, options: .MutableContainers, error: &err) as NSDictionary
                    
                    let parseJSON = json["ID"] as? NSInteger
                    
                    
                    println("parse id:\(parseJSON )")
                    
                    
                    if parseJSON != -1
                    {
                        //auth successesfull
                        NSUserDefaults.standardUserDefaults().setBool(true, forKey:"isUserLoggedIn");
                        NSUserDefaults.standardUserDefaults().synchronize();
                        self.dismissViewControllerAnimated(true, completion: nil)
                    }
                    else
                    {
                  self.dispayMyAlertMassage ("Введите данные пользователя!");  -- тут система ломается
                    }
            }
            task.resume()
        }

here is the error text:
2015-08-03 17:11:01.440 VinciMobile[5879:423254] *** Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /SourceCache/UIKit_Sim/UIKit-3318/Keyboard/UIKeyboardTaskQueue.m:374
2015-08-03 17:11:01.444 VinciMobile[5879:423254] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

It was revealed in an imperial way that it was this line that breaks everything
self.presentViewController(myAlert,animated: true, completion : nil)

Help me understand what is wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mechanicnikodimich, 2015-08-03
@mechanicnikodimich

My own director.
That's right.

dispatch_async(dispatch_get_main_queue(), {
                        self.dispayMyAlertMassage ("Неверные регистрационные данные");
                        });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question