gpt4 book ai didi

ios - Swift Firebase保存/更新具有相同子值的多个父项

转载 作者:行者123 更新时间:2023-11-29 05:54:50 25 4
gpt4 key购买 nike

我有一个字符串数组,它是用户的“uid”。我正在尝试将数据/子项附加到这些多个“uid”。向个别家长/用户添加 child 或更新 child 很容易,我知道如何做到这一点。问题是该数组可以包含 1 个 uid 或 50 个 uid。我是否可以获取这些 uid,然后用相同的值更新它们?我不确定要提供什么代码,因为我只是想尽一切办法来解决这个问题。

使用下面的代码,这是我向其他用户发送消息。

uid 字符串数组

var data = [String]()

我向 2 位用户发送消息的示例代码,只是想在此处提供一些内容以表明我知道如何更新/保存数据

private func sendMessageWithProperties(_ properties: [String: Any]) {
let businessRef = Database.database().reference().child("Business Group Chats Messages").child((group?.uid)!).child((Auth.auth().currentUser?.uid)!)
let ref = Database.database().reference().child("Business Group Chats Messages").child((Auth.auth().currentUser?.uid)!).child((group?.businessName)!)
let businesChildRef = businessRef.childByAutoId()
let childRef = ref.childByAutoId()
let fromID = Auth.auth().currentUser!.uid
let timeStamp = Int(Date().timeIntervalSince1970)
var value:[String: Any] = ["fromId" : fromID, "timeStamp" : timeStamp, "name": self.loggedInUserData?["name"] as? String]

properties.forEach { (k,v) in
value[k] = v
}

childRef.updateChildValues(value) { (err, ref) in
if err != nil {
print(err!)
return
}

Database.database().reference().child("Business Group Chats").child((self.group?.uid)!).child((Auth.auth().currentUser?.uid)!).updateChildValues(["last message" : childRef.key!, "timestamp" : timeStamp, "businessName":(self.group?.businessName)!])

Database.database().reference().child("Business Group Chats").child((Auth.auth().currentUser?.uid)!).child((self.group?.uid)!).updateChildValues(["last message" : childRef.key!, "timestamp" : timeStamp])

self.inputContainerView.inputTextField.text = nil
}
}

最佳答案

这是我获取“uid”数组,然后拉出并打印我可以通过字符串数组访问每个“uid”。允许我访问,现在我可以将数据附加到每个。

Database.database().reference().child("Businesses").observe(.value, with: { snapshot in
if snapshot.exists() {
self.businessUID = snapshot.value as? NSDictionary
if let dict = snapshot.value as? NSDictionary {
for item in dict {
let json = JSON(item.value)
let businessUid = json["uid"].stringValue
for uid in self.data {
if uid == businessUid {
//print(uid)
self.businessessuids = uid
print(self.businessessuids)

Database.database().reference().child("Businesses").child(self.businessessuids).observe(.value, with: { snapshot in
print(snapshot)
print("Trying to pull data from multiple strings right here this shoudld work")
})
print("printing the values to match with the string UID's")
}
}
}
}
} else {
print("does not exist")
}
})

关于ios - Swift Firebase保存/更新具有相同子值的多个父项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55265399/

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