Retrieving common values in Firebase

孤人 提交于 2019-12-24 06:29:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!