0

У меня есть код

    @IBAction func joinButtonTouch(sender: AnyObject) {
    NSOperationQueue.mainQueue().addOperationWithBlock({
        self.joinButton.backgroundColor = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1.0)
        self.activityIndicator.startAnimating()
        self.resultLogIn.text = "Происходит проверка данных..."
        })


    var response = NetworkManager.sharedInstance.SendRequest("<Command=LogIn><Login=\(loginTextField.text!)><Password=\(passwordTextField.text!)>")
}

Я хочу, чтобы ДО того, как начнется загрузка данных о пользователе, у меня начинал работать activityIndicator. Но этого не происходит. Почему? Я ведь занусул его в UI поток. Он начинает крутиться уже после загрузки данных.
SendRequest:

func SendRequest(request: String) -> String
{
    var response = ""
    var bigData: [UInt8] = []
    var (success, errmsg) = client.connect(timeout: 1)
    if success {
        var (success, errmsg) = client.send(str: request + "\n" )
        if success {
            while(true) {
                var data = client.read(1024*10)
                if (data == nil) {
                    break
                }
                bigData.appendContentsOf(data!)
            }
        }
        else {
            print(errmsg)
        }
    }
    else {
        print(errmsg)
    }
    var bytes = NSData.init(bytes: bigData, length: bigData.count)
    if let str = String(data: bytes, encoding: NSUTF8StringEncoding) as String? {
        print(str)
        response = str
    }
    return response
}
0

1 ответ 1

0

Включить таймер можно без NSOperationQueue. В NSOperationQueue лучше включить SendRequest, а в основную очередь после скачивания добавить выключение таймера.

Более подробно можно почитать здесь: http://www.appcoda.com/ios-concurrency/

@IBAction func joinButtonTouch(sender: AnyObject) {
    self.joinButton.backgroundColor = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1.0)
    self.activityIndicator.startAnimating()
    self.resultLogIn.text = "Происходит проверка данных..."
    NSOperationQueue.addOperationWithBlock{ () -> Void in
        var response = NetworkManager.sharedInstance.SendRequest("<Command=LogIn><Login=\(loginTextField.text!)><Password=\(passwordTextField.text!)>")
        NSOperationQueue.mainQueue().addOperationWithBlock({
            self.activityIndicator.stopAnimating()
        })
    } 
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.