【发布时间】:2018-03-09 13:45:05
【问题描述】:
我在尝试同时更新 Firebase 中不同 JSON 分支的值时遇到了一个特殊问题。文档中提供的方法非常适合创建新数据,
let key = ref.child("posts").childByAutoId().key
let post = ["uid": userID,
"author": username,
"title": title,
"body": body]
let childUpdates = ["/posts/\(key)": post,
"/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)
但是当我尝试在多个位置更新数据时,它会覆盖其他键,就好像我在使用 setValue 一样。这是我的代码。
let userID = Auth.auth().currentUser?.uid
userRef = Database.database().reference()
let vehicle = ["make":make.text,
"model": model.text,
"year":year.text,
"color":color.text,
"doors":doors.text]
let driver = ["currentEvent":eventID]
let childUpdates = ["/users/\(userID!)": driver,
"/status/\(userID!)/driverInfo/vehicle": vehicle]
userRef.updateChildValues(childUpdates)
我还附上了一张图片,显示了执行该功能时哪些数据被删除了,哪些数据没有被删除。
我相信我正在尝试做的事情是可能的,据我所知,updateChildValues 的全部目的是为了不覆盖其他孩子。
感谢任何帮助。谢谢!
【问题讨论】:
标签: json swift firebase firebase-realtime-database