从Firebase中提取所有常用元素的最有效方法是什么?我正在尝试拉出postName相同的所有帖子并获取其各自的userId并将其附加到数组中。根据下面的数据库结构,输出应为[“userId1”,“userId2”,“userId4”,“userId5”]。
func fetchPosts() {
let usersRef = Database.database().reference().child("posts")
usersRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children.allObjects as! [DataSnapshot] {
let value = child.value as? NSDictionary
let postName = value?["postName"] as? String ?? ""
//check for postName that have the same value
//for each of those postName, fetch their relevant userId
}
})
}
数据库结构
_Posts
__postId1
____postName: "Common"
____userId: "userId1"
__postId2
____postName: "Common"
____userId: "userId2"
__postId3
____postName: "Not common"
____userId: "userId3"
__postId4
____postName: "Also common"
____userId: "userId4"
__postId5
____postName: "Also common"
____userId: "userId5"