bibscy
bibscy

Reputation: 2708

Why does updateChildValues overwrites instead of updating data in Firebase?

I am trying to update a node in Firebase Database using .updateChildValues method, but it overwrites the whole node instead of updating it with the new values. Does anyone why this happens?

func writeToDB() {
   let path = "FeesCleaner/\(self.fromUidString.text!)"
    let childUpdates = [path:  self.feesCleaner_Object] as [String : Any]
        self.dbRef.updateChildValues(childUpdates, withCompletionBlock: { (error, response) in
        })
 }

//self.feesCleaner_Object is shown below in JSON format.

"FeesCleaner" : {
  "05MSPgkP1ddhFqXDRjIB4npGEPV2" : {
    "BackgroundCheck" : {
      "FeeAmount" : "30",
      "FeeStatus" : "false",
      "TimeStampProfileCreated" : "2992939382"
   },
    "OutstandingFees" : {
      "BalanceCarriedForwardAmount" : "0.0",
      "PreviousBalanceCarriedForward" : "0.0",
      "TimeStampFeesCarriedForward" : "1520287823"
   },
   "TimeStampFeesSaved" : {
     "1520287823" : {
       "BalanceCarriedForwardAmount" : "0.0",
       "PayPeriodTimeStampStartDate" : "1510531260",
       "PaymentRef" : "354576819",
       "PreviousBalanceCarriedForward" : "0.0",
       "TimeStampDisbursedPayment" : "1520287823",
       "TotalAmountDebtToCleaner" : "0.0",
       "TotalAmountFeesCurrentPayPeriod" : "0.0",
       "TotalAmountPaidToCleanerForAllBookings" : "0.0",
       "TotalAmountProfitToCleanerForAllBookings" : "0.0"
     }
   }
  }
},

Upvotes: 1

Views: 639

Answers (1)

Parrett Apps
Parrett Apps

Reputation: 589

Given a single key path, updateChildValues only updates data at the first child level, and any data passed in beyond the first child level is a treated as a setValue operation. Multi-path behavior allows longer paths to be used without overwriting data. Take a look at the example and related documentation: https://www.firebase.com/docs/ios/guide/saving-data.html#section-update

Upvotes: 2

Related Questions