【发布时间】:2020-03-05 04:23:52
【问题描述】:
我正在尝试使用 childByAutoID 和 updateChildAt() 更新我的 firebase 数据库中的子节点。当我执行代码时,类型会更新,但是它位于节点之外,如下所示。
type: 3 应该在表示 type: 2 的节点内更新
我用来执行这个的代码看起来像
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")
})
}
这只是将类型设置为节点下。我尝试使用下面的代码,但在下面的行中,我收到错误 Cannot convert value of type 'String' to expected argument type '[AnyHashable: Any]'
WORK_PROG_REF.child(uid!).updateChildValues("type").setValue(3)
感谢大家的帮助
【问题讨论】:
标签: ios xcode firebase firebase-realtime-database