【发布时间】:2018-01-28 06:30:31
【问题描述】:
我有一个应用程序,当用户单击 UIButton 时, 1. 我在 Firebase 中更新了一个孩子(比如 tapCount)
- 加载带有图像的 ViewController。
我正在使用事务块来增加这个计数,因为多个用户可能同时增加这个计数。我注意到是因为完成此 Firebase 事务有一点延迟,应用程序等待完成它,然后在单击图像时加载视图控制器。它几乎使应用程序有点迟钝。那么在不引起任何 UI 问题的情况下,推荐的方法是什么?有没有办法在后台线程中运行它?谢谢
func incrementTapCount() {
_tapCount += 1
// update the link
ref.runTransactionBlock { (currentData:MutableData) -> TransactionResult in
currentData.childData(byAppendingPath: "tapCount").value = self._tapCount
return TransactionResult.success(withValue: currentData)
}
}
func moreCommentAction(_ sender: AnyObject) {
let postCategory = self.globalPost.postCat
if Auth.auth().currentUser?.uid != nil {
//user is logged in
// this increments the Like count when a button is tapped.
self.globalPost.incrementTapCount()
// let likeCount = self.globalPost.favoriteBoost + self.globalPost.favoriteDict.count + self.globalPost.tapCount
// likeButton.setTitle(" \(likeCount)", for: UIControlState())
}
else {
//user is not logged in, so skip incrementing the tapCount.
}
let moreCommentVC = sender.storyboard?!.instantiateViewController(withIdentifier: "MoreCommentViewController") as! MoreCommentViewController
moreCommentVC.globalPost = self.globalPost
sender.present(moreCommentVC, animated: true, completion: nil)
}
【问题讨论】:
-
你可以先打开viewcontroller,然后从viewcontroller更新count
-
如果我在其中添加了它,比如在 viewdidLoad 或 viewDidAppear 中,不会仍然有相同的延迟吗?
-
runTransactionBlock是异步的,这意味着它会立即返回 -
嗯,看起来不像,因为如果是这样,那么我一开始就不会遇到这个问题。我尝试在另一个控制器的 viewdidLoad 中添加它,但我看到了同样的问题。就像它等待它返回一样,这确实需要 1-2 秒。我需要的是 - 我更新它,我不想等待。如果它不成功,它的罚款。
-
你能发布更多代码吗?
标签: swift firebase firebase-realtime-database