gpt4 book ai didi

json - Swift:Firebase updateChildValues 函数在位置覆盖和删除其他键

转载 作者:行者123 更新时间:2023-11-28 15:18:18 27 4
gpt4 key购买 nike

尝试在 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)

我还附上了一张图片,显示了执行该功能时哪些数据被删除,哪些数据没有被删除。

Image revealing the data which is deleted

我相信我正在尝试做的事情是可能的,据我了解,updateChildValues 的全部目的是为了不让其他 child 被覆盖。

感谢任何帮助。谢谢!

最佳答案

当前您正在用这样的对象更新对象:

let childUpdates = ["/users/\(userID!)": driver,
"/status/\(userID!)/driverInfo/vehicle": vehicle]

您将对象(字典)作为 childUpdates 字典的值。这样做是用这个对象替换所有子对象,这意味着删除您发送 nil 的值,例如在您没有包含 infoinfoThat 的情况下。

现在,如果您只想更改您想要提供的值,例如只更改 say makemodel 的值,用于 vehiclecurrentEvent 对于 driver ,你必须给出这些值的特定路径

["/users/\(userID!)/currentEvent": eventID,
"/status/\(userID!)/driverInfo/vehicle/make": make.text, "/status/\(userID!)/driverInfo/vehicle/model": model.text]

我认为这将按您的预期更新这些位置的值。

HTH...

关于json - Swift:Firebase updateChildValues 函数在位置覆盖和删除其他键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46458568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com