3

I am following this document. Following is my code for update:

func updateDealResultToServer(key:String,dealResult : String)
{

    let post = ["dealResul": dealResult]
    let childUpdates =
        ["/komal_xyz/\(key)": post
        ]

    rootRef.updateChildValues(childUpdates)

}

This Is my Firebase database structure: enter image description here

I want to only change the value for dealResult. Whenever I try to run above code for particular child node like 1473670100726, other value except dealResul is deleted.

AL.
  • 36,815
  • 10
  • 142
  • 281
Komal Kamble
  • 424
  • 1
  • 8
  • 25
  • @omal Kamble:when i remove value from listing from one device that value remove from another device at same time ?so how that happen ? please explain for android. i dont understand how work? – Sachin Suthar Aug 08 '17 at 11:17

3 Answers3

7

For Updating values at a particular node in Firebase Realtime Database, use:-

  • You can either use runTransactionBlock:

      func updateTotalNoOfPost(completionBlock : (() -> Void)){
    
    
    let prntRef = FIRDatabase.database().reference().child("komal_kyz").child(your_AuroID).child("dealResul")
    
    prntRef.runTransactionBlock({ (resul) -> FIRTransactionResult in
        if let dealResul_Initial = resul.value as? Int{
    
            //resul.value = dealResul_Initial + 1
            //Or HowSoEver you want to update your dealResul. 
             return FIRTransactionResult.successWithValue(resul)
        }else{
    
            return FIRTransactionResult.successWithValue(resul)
    
        }
        }, andCompletionBlock: {(error,completion,snap) in
    
                print(error?.localizedDescription)
                print(completion)
                print(snap)
            if !completion {
    
               print("Couldn't Update the node")
            }else{
    
                completionBlock()
            }
        })
    
     }
    

    While calling this function:-

    updateTotalNoOfPost{
           print("Updated")
          }
    
  • Or just call updateValues

        let prntRef  = FIRDatabase.database().reference().child("komal_kyz").child(your_AuroID)
        prntRef.updateChildValues(["dealResul":dealResult]) 
    

PS:- Prefer using runTransactionBlock: instead of .updateChildValues if you only want to increment a particular node. Also read this: -https://stackoverflow.com/a/39458044/6297658

Community
  • 1
  • 1
Dravidian
  • 9,945
  • 3
  • 34
  • 74
5

I suggest using setValue for single child updates (keep in mind that I am using Swift 3, slight syntax changes may apply but you should be fine):

rootRef.child("komal_kyz").child(key).setValue(["dealResul":dealResult])
tech4242
  • 2,348
  • 2
  • 23
  • 33
1
let ref = FIRDatabase.database().reference().root.child("users").child("childKey").updateChildValues(["childKeyForUpdate": "NewData"])
  • Would you please elaborate on that? – mrun Feb 09 '18 at 09:30
  • Can you provide some details? – aircraft Feb 09 '18 at 11:02
  • firebase is a database which is used for implement chatting functionality in your application , firebase provide socket programming. if you want to update value of any selected chilled then this code help-full for you in which "user" is a your table name on firebaseDataBase and "childKey" is a chilled of user table and "childKeyForUpdate" is your selected key for update and the "NewData" id your latest data for replace to old – Er. Deependra Thakur May 11 '18 at 09:09