为什么不起作用 NSOperationQueue.mainQueue?
Why doesn't work NSOperationQueue.mainQueue?
我有这个代码
@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 = "Checking data..."
})
var response = NetworkManager.sharedInstance.SendRequest("<Command=LogIn><Login=\(loginTextField.text!)><Password=\(passwordTextField.text!)>")
}
我想要 activityIndicator
运行 BEFORE 启动 SendRequest
方法。但这没有用。为什么? activityIndicator
运行 当 SendRequest
完成时。
您正在用 SendRequest
阻塞主线程。您还安排了启动指标的操作,但它在当前正在发生的事情之后排在队列中,因此在您释放主线程之前它不会运行。
因此,不要将指标更改放入操作中,将 SendRequest
放入操作中并在非主队列(即后台线程)上执行它。那么一切都会很顺利,用户也会很开心。
我有这个代码
@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 = "Checking data..."
})
var response = NetworkManager.sharedInstance.SendRequest("<Command=LogIn><Login=\(loginTextField.text!)><Password=\(passwordTextField.text!)>")
}
我想要 activityIndicator
运行 BEFORE 启动 SendRequest
方法。但这没有用。为什么? activityIndicator
运行 当 SendRequest
完成时。
您正在用 SendRequest
阻塞主线程。您还安排了启动指标的操作,但它在当前正在发生的事情之后排在队列中,因此在您释放主线程之前它不会运行。
因此,不要将指标更改放入操作中,将 SendRequest
放入操作中并在非主队列(即后台线程)上执行它。那么一切都会很顺利,用户也会很开心。