我发现NSBlockOperation
在我的应用程序中更新 UI 时看到不同的行为很奇怪。
对于每个UIButtons
(例如拇指向上、拇指向下、喜欢等),我将它们链接到它们的操作方法,类似于下面使用NSBlockOperation
.
以Like按钮为例:
@IBAction func likePost(sender: AnyObject) {
favoriteBtn.enabled = false
let operation = NSBlockOperation { () -> Void in
NetworkUtils.sharedInstance.likePost(self.curPost) { (completed, error) -> Void in
self.favoriteBtn.enabled = true
self.updateUserLabels(completed) // does not get triggered
}
}
operationQueue.addOperation(operation)
}
对ParseNetworkUtils
进行 API 调用,返回(Bool) 和(String?) 的闭包。在此,我使用以下方法更新 UI :completed
error
NSBlockOperation
self.updateUserLabels()
mainQueue
func updateUserLabels(completed: Bool) {
if completed {
NSOperationQueue.mainQueue().addOperationWithBlock { () -> Void in
self.likeCount.text = self.curPost.likes;
}
}
}
该代码在iPhone 模拟器 5s中运行良好,并且符合预期,但是,在模拟器 6s 或 6s Plus 上,需要更长的时间才能看到Like
重新启用按钮,并且updateUserLabels()
甚至在NetworkUtils.sharedInstance.likeQuestion()
调用返回闭包之前调用该方法。
我想知道为什么在不同的 iPhone 模拟器上会有如此大的差异,以及如何让它们像在 5s 上一样工作?