【问题标题】:Firebase runTransactionBlock() deletes nodeFirebase runTransactionBlock() 删除节点
【发布时间】:2015-11-28 01:48:30
【问题描述】:

我正在使用适用于 iOS 的 Firebase SDK (2.4.2)。以下代码是SLComposeServiceViewController 对象中使用的didSelectPost() 函数的一部分。非常重要的一点是要指出这种行为在普通的UIViewController 中使用时是不一样的(我知道很奇怪)。

假设基本代码如下:

    override func presentationAnimationDidFinish() {
        // Retrieving content and identifying type here
    }

    override func didSelectPost() {
        self.myRootRef.runTransactionBlock({
            (currentData:FMutableData!) in
                currentData.value = "test"

                // Finalizing the transaction
                return FTransactionResult.successWithValue(currentData)
            }
        )

        self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
    }

事务完成后,node_value 将从 Firebase 中完全消失。该值未设置,节点被删除。这是一个非常奇怪和意想不到的行为!

另一方面,以下代码按预期工作。

    override func didSelectPost() {
        self.myRootRef.setValue("test", withCompletionBlock: {
            (error:NSError?, ref:Firebase!) in
            if (error != nil) {
                print("Data could not be saved.")
            } else {
                print("Data saved successfully!")
            }
        })

        self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
    }

几天前还没有这个问题,最后我运行了上面的代码。关于问题所在的任何见解?

【问题讨论】:

  • 如果我的回答对您有用,请告诉我。如果是,请将其标记为已接受。保持未回答的队列清晰是件好事。
  • @DavidEast 我还没有时间测试答案。这个周末我很可能会这样做,之后我会在这里确认答案。

标签: ios swift debugging firebase


【解决方案1】:

如果您将 nil 设置为 Firebase 数据库中的路径,它的作用与 .removeValue() 相同。

在运行事务时,您应该检查nil因为如果没有写入默认值,则可以使用nil 调用事务。

myRootRef.runTransactionBlock({
    (currentData:FMutableData!) in
    var value = currentData.value as? Int
    if value == nil { 
        value = 0
    }
    currentData.value = value! + 1
    return FTransactionResult.successWithValue(currentData)
}) 

阅读Firebase Docs了解更多信息。

【讨论】:

  • 这并没有解决问题。我相信我正在使用的场景比问题所暗示的变量更多。有另一个应用程序订阅了此节点的更改事件。当发生更改时,它会抓取数据并清除内容。然而,直到最近,这才成为一个问题。
猜你喜欢
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 2016-10-31
  • 2017-03-19
  • 1970-01-01
  • 2014-12-19
相关资源
最近更新 更多