【问题标题】:runTransactionBlock causing delays in main threadrunTransactionBlock 导致主线程延迟
【发布时间】:2018-01-28 06:30:31
【问题描述】:

我有一个应用程序,当用户单击 UIButton 时, 1. 我在 Firebase 中更新了一个孩子(比如 tapCount)

  1. 加载带有图像的 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


【解决方案1】:

我可以看到与 obj-c 相同的问题。 runTransactionBlock() 立即返回,但在调用 andCompletionBlock() 之前,主线程被阻塞 1-2 秒。

以前没有发生过,更新到较新的 firebase 版本后开始看到它。就我而言,我设法通过减少要更新的数据量来缩短这种延迟。似乎 firebase 正在主线程上执行类似本地缓存更新的操作(只是一个理论)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多