问题
What's the most efficient way to pull all common elements from Firebase? I'm trying to pull all posts where the postName is the same and get their respective userId and append it into an array. Based on the database structure below, the output should be ["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
}
})
}
Database structure
_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"
来源:https://stackoverflow.com/questions/47763444/retrieving-common-values-in-firebase