为什么我的Firebase交易中的currentData总是零? [英] Why is the currentData in my Firebase transaction always nil?

查看:110
本文介绍了为什么我的Firebase交易中的currentData总是零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是数据库如何通过Firebase呈现的

 父级
匹配
用户1
对手:用户2
状态:NotReady
用户2
对手:用户1
状态:NotReady
pre>

我试图用RunTransaction更新 State (每个用户)的值。



我试图做的是:


  • 检查物品如果他不存在 - 更新值

  • 如果他不存在 - 操作UI
  • 如果他突然删除 - 中止交易(可能的条件 - 如果两个关闭事件同时运行,一个更新状态和其他删除全部密钥(<$ c
    $ b $ let path =父/匹配/用户1($ / $ c> removeValue) / state
    $ b $ futureRef = Firebase(url:path)

    futureRef.runTransactionBlock({
    (currentData:FMutableData!)let value = currentData.value as?String

    if value!= nil {
    currentData.value =Ready

    return FTransactionResult.successWithValue(currentData)
    }

    返回FTransactionResult.abort()

    }和CompletionBlock:{
    //完成检查
    (错误:NSError!,成功:布尔,数据:FDataSnapshot!)在

    如果错误== nil&&成功&& data.value as! String ==Ready
    {
    //值不为空(未删除),并准备好
    ManipulateUI()

    }
    else
    {

    //值删除
    }


    }




    但是由于某种原因,我得到的是 nil 并直接转到中止任何建议? ,我将在下面介绍。它解释了为什么你得到 nil ,但不幸的是,你的当前方法将无法正常工作。



    调用 runTransactionBlock ,Firebase客户端将立即调用您的块,并以当前的位置值为最佳猜测值。这个最好的猜测可能是正确的,但如果它不知道该位置的当前值,将是 nil

    < Firebase客户端然后发送给您的块的当前值和您返回给服务器的值。 Firebase服务器会检查数据库中的当前值是否与客户的最佳猜测相同。如果是这样,它会写入您指定的新值。如果数据库中的值不同,则服务器拒绝新值,并将拒绝和实际当前值从数据库发送到客户端。



    您的块被再次调用,但是这次是针对当前值新的最佳猜测。

    <这个过程一直持续到服务器接受这个操作,或者直到有太多的尝试(我认为是10或25)。

    This is how the DB presented over Firebase

    Parent
          Match
               User 1
                     Opponent : User 2
                     State    : "NotReady"
               User 2 
                     Opponent : User 1
                     State    : "NotReady"
    

    I'm trying to update the values of State (each user individually) with RunTransaction.

    What I was trying to do :

    • Check that the item haven't removed(is not nil/null)
    • If he is exist - update the value
    • if he doesnt exists - manipulate UI
    • if he suddenly deleted - abort the transaction (Possible condition - if two close event running at the same time, one updating "State" and other deletes the all key(removeValue)

       let path =  "Parent/Match/User 1/state"
      
       let futureRef = Firebase(url: path)
               futureRef.runTransactionBlock({
          (currentData:FMutableData!) in
          let value = currentData.value as? String
      
          if value != nil  {
              currentData.value = "Ready"
      
              return FTransactionResult.successWithValue(currentData)
          }
      
              return FTransactionResult.abort()
      
          }, andCompletionBlock: {
              // Completion Check
              (error:NSError!, success:Bool, data:FDataSnapshot!) in
      
              if error == nil && success && data.value as! String == "Ready"
              {
                  //Value is not null(not removed) and he is ready
                    ManipulateUI()
      
              }
              else 
            {
      
              //Value deleted
             }
      
      
              }
          )
      

    But for some reason - I'm getting currentData that is nil and going straight to the Abort. Any suggestions? Thanks!!!

    解决方案

    Firebase uses a compare-and-set approach to transactions, which I'll describe below. It explains why you're getting nil, but unfortunately means your current approach won't work.

    When you invoke runTransactionBlock, the Firebase client will immediately invoke your block with its best guess for the current value of the location. This best guess may be correct, but will be nil if it doesn't know a current value for the location.

    The Firebase client then sends the current value it gave your block and the value that you returned to the server. The Firebase server checks if the current value in the database is the same as the client's best guess. If so, it writes the new value you specified. If the value in the database is different, the server rejects the new value and send the rejection and the actual current value from the database to the client.

    Your block is invoked again, but this time with the new "best guess" as to the current value.

    This continues until either the server accepts the operation or until there have been too many tries (something 10 or 25 I think).

    这篇关于为什么我的Firebase交易中的currentData总是零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆