How to update particular value of child in Firebase DB

前端 未结 3 1640
南旧
南旧 2020-12-29 17:21

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

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

    let post = [\"dealResul\"         


        
相关标签:
3条回答
  • 2020-12-29 17:27

    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])
    
    0 讨论(0)
  • 2020-12-29 17:43
    let ref = FIRDatabase.database().reference().root.child("users").child("childKey").updateChildValues(["childKeyForUpdate": "NewData"])
    
    0 讨论(0)
  • 2020-12-29 17:44

    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

    0 讨论(0)
提交回复
热议问题