Reputation:
I am trying to update a child node in my firebase database by using childByAutoID and updateChildAt(). When I execute the code the type is updated, however it is outside of the node as seen below.
the type: 3 should be updated inside of the node that says type: 2
the code that I am using to execute this looks like
let DB_REF = Database.database().reference()
let WORK_PROG_REF = DB_REF.child("workInProgress")
func completeJobProgress() {
let workUser = self.workerUser
let uid = Auth.auth().currentUser?.uid
let workerId = workUser?.uid
let address = workerUser?.address
let createDate = Int(NSDate().timeIntervalSince1970)
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String : Any] else { return }
let user = User(dictionary: dictionary as [String : AnyObject])
workUser?.uid = snapshot.key
let docData: [String: Any] = ["workerId": uid!,
"creationDate": createDate,
"fromId" : workerId!,
"location": address!,
"type": 3,
"checked": 0,]
self.setJobToCompletedInDatabase(uid: uid!, values: docData as [String : AnyObject])
self.postJobNotificationsIntoDatabseWithUID(uid: workerId!, values: docData as [String : AnyObject])
//how I initially got the type: 3 to save under the node was by using the line below
WORK_PROG_REF.child(uid!).child("type").setValue(3)
print(workerId!)
}, withCancel: { (err) in
print("attempting to load information")
})
print("Finished saving user info")
self.dismiss(animated: true, completion: {
print("Dismissal complete")
})
}
this just sets the type to be under the node. I tried using the code below but with the line below, I am getting the error Cannot convert value of type 'String' to expected argument type '[AnyHashable: Any]'
WORK_PROG_REF.child(uid!).updateChildValues("type").setValue(3)
Thanks for any and all help
Upvotes: 0
Views: 98
Reputation: 3086
Try:
Solution With Transaction:
WORK_PROG_REF.child(uid!).child("-M1bOYAcP_IMFbgz8siD").runTransactionBlock({ (result: MutableData) -> TransactionResult in
if var objInfo = result.value as? [String: Any] {
objInfo["type"] = 3
// Set value and report transaction success
result.value = objInfo
return TransactionResult.success(withValue: result)
}else{
return TransactionResult.success(withValue: result)
}
}) { (error,completion,snap) in
//Handle Error
}
Solution without transaction:
WORK_PROG_REF.child(uid!).child("-M1bOYAcP_IMFbgz8siD"). child("type").setValue(3)
I hope this will resolve your issue.
Upvotes: 1