Why is the current date in my Firebase transaction always zero?

This is how DB presented over Firebase

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

I am trying to update values State(each user separately) using RunTransaction.

What I tried to do:

  • Make sure the item is not deleted (not nil / null)
  • If it exists, update the value.
  • if it does not exist - manipulate the user interface
  • if he suddenly left - abort the transaction (Possible condition - if two closing events are executed simultaneously, one update "Status" and the other deletes all keys (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 get currentData, which nilgoes directly to Abort. Any suggestions? Thanks!!!

+4
1

Firebase , . , nil, , , .

runTransactionBlock Firebase . , nil, .

Firebase , , , . Firebase , , . , , . , .

, " " .

, (- 10 25, ).

+3

Source: https://habr.com/ru/post/1624378/


All Articles