gpt4 book ai didi

ios - 如何检查 firebase 子节点的特定值(UID)?

转载 作者:行者123 更新时间:2023-11-28 06:20:51 25 4
gpt4 key购买 nike

我正在尝试查询 Firebase 以检查当前用户的 uid 是否列在房间的“参与者”中。如果是这样,我会获取该房间的信息。

下面是我当前的观察者,它监听应用程序中的所有房间,而不仅仅是用户参与的房间。但是我需要先进行检查,查看当前用户的 UID 在哪些房间中列出;如果匹配(uid 在房间的参与者中),则获取该房间的数据:

private func observeRooms() {

guard let uid = FIRAuth.auth()?.currentUser?.uid else {print("Error getting user UID"); return}

roomRefHandle = roomRef.observe(.childAdded, with: { (snapshot) -> Void in

let roomData = snapshot.value as! Dictionary<String, AnyObject>
let id = snapshot.key
guard let name = roomData["roomName"] as! String! else {print("Error getting user name"); return}

self.usersRooms.append(Room(id: id, name: name, participants: [uid]))

self.tableView.reloadData()
})
}

这是房间在数据库中的结构:

"rooms" : {
"-Ki6TJWO-2R1L4SyhSqn" : {
"messages" : {
"-Ki6TWrXxWqjaRJAbyVt" : {
"senderId" : "tzfHgGKWLEPzPU9GvkO4XE1QKy53",
"senderName" : "Timothy",
"text" : "Room One message"
}
},
"participants" : {
"tzfHgGKWLEPzPU9GvkO4XE1QKy53" : true
},
"roomName" : "Room One"
},
"-Ki6TKOnmToeUuBzrnbb" : {
"participants" : {
"tzfHgGKWLEPzPU9GvkO4XE1QKy53" : true
},
"roomName" : "Room Two"
},
"-Ki6TLGC1Encm1v-CbHB" : {
"participants" : {
"tzfHgGKWLEPzPU9GvkO4XE1QKy53" : true
},
"roomName" : "Room Three"
}
}

我如何更改我的函数,以便它在获取值之前首先检查所有房间的参与者是否有当前用户的 uid?

感谢您的任何建议!

编辑:Jay 的方法:

private func observeRooms() {

guard let uid = FIRAuth.auth()?.currentUser?.uid else {print("Error getting user UID"); return}
let queryRef = roomRef.queryOrdered(byChild: "participants/\(uid)").queryEqual(toValue: true)

queryRef.observe(.childAdded, with: { snapshot in

let roomDict = snapshot.value as! [String: AnyObject]

let id = snapshot.key

let roomName = roomDict["roomName"] as! String
let participants = roomDict["participants"] as! [String: AnyObject]
let numberOfParticipants = participants.count

print("\nRoom Name: \(roomName)")
print("Participants: \(participants)")
print("Room ID: \(id)\n")

self.usersRooms.append(Room(id: id, name: roomName, participants: [uid]))
self.tableView.reloadData()
})
}

最佳答案

为了保持 super 简单,在用户所属的每个房间中存储对用户的引用。

rooms
room_0
room_name: "My Room"
users
user_1: true
user_2: true
room_1
room_name: "Their Room"
users
user_1: true

然后一个简单的查询将收集所有需要的数据,并且还会附加一个观察者,因此如果该用户加入任何新房间,应用程序将收到通知。

let roomsUsersRef = self.ref.child("rooms")
let queryRef = roomsUsersRef.queryOrdered(byChild: "users/user_1").queryEqual(toValue: true)

queryRef.observe(.childAdded, with: { snapshot in

let roomDict = snapshot.value as! [String: AnyObject]
let roomName = roomDict["room_name"] as! String

let usersDict = roomDict["users"] as! [String: AnyObject]
let userCount = usersDict.count

print("Room: \(roomName) has \(userCount) users")
})

和输出

Room: My Room has 2 users
Room: Their Room has 1 users

您可以使用 .childChanged 和 .childRemoved 对此进行扩展,以跟踪该用户所属房间中发生的任何事件。因此,如果另一个用户加入或离开该用户所在的房间,应用程序将收到通知;如果房间的所有者将此用户从房间引导,该应用程序也会收到通知。

关于ios - 如何检查 firebase 子节点的特定值(UID)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43503726/

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