Swift Firebase使用相同的子值保存/更新多个父项

时间:2019-03-20 16:12:24

标签: ios swift firebase firebase-realtime-database

我有一个字符串数组,它是用户的“ uid”。我正在尝试将数据/孩子添加到这些多个“ uid”中。向单个父母/用户添加孩子或更新孩子很容易,我知道该怎么做。问题在于该数组可以包含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
    }
}

1 个答案:

答案 0 :(得分:0)

这是我带上“ uid's”数组,然后提取并打印,我可以通过字符串数组访问每个“ 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")
        }
    })