Is it possible to read from multiple child nodes?

烂漫一生 提交于 2020-02-16 06:25:26

问题


I want to read all three data sourcing from "Arts & Humanities" and "Beauty & Style". Is this possible?

Let ref = Database.database().reference().child("posts")
//CODE A: Pulls 2 snapshot, but doesn't display anything

let ref = Database.database().reference().child("posts").child("Arts & Humanities")
//CODE B: only pulls up the two feeds but excludes beauty and style.  Vice versa

//Below is the listener code I have. This works only works with CODE B above; but ideally id like to read the post under "Beauty & Style" as well.

postsRef.observeSingleEvent(of: .value, with: { snapshot in
    var tempPosts = [PostModel]()

    for child in snapshot.children {
        print(snapshot.childrenCount)
        if let childSnapshot = child as? DataSnapshot,

            let dict = childSnapshot.value as? [String:Any],
            let author = dict["author"] as? [String:Any],

            let uid = author["uid"] as? String,
            let username = author["username"] as? String,
            let fullname = author["fullname"] as? String,
            let patthToImage = author["patthToImage"] as? String,
            let url = URL(string:patthToImage),

            let pathToImage = dict["pathToImage"] as? String,
            let likes = dict["likes"] as? Int,
            let postID = dict["postID"] as? String,
            let message = dict["message"] as? String,
            let genre = dict["genre"] as? String,
            let timestamp = dict["timestamp"] as? Double {

            if childSnapshot.key != lastPost?.id {
                let userProfile = UserProfile(uid: uid, fullname: fullname, username: username, patthToImage: url)
                let post = PostModel(genre: genre, likes: likes, message: message, pathToImage: pathToImage, postID: postID, userID: pathToImage, timestamp: timestamp, id: childSnapshot.key, author: userProfile)
                tempPosts.insert(post, at: 0)
                if lastPost?.id != nil {
                    lastPostIdChecker = lastPost!.id
                }
            }
        }
    }
    return completion(tempPosts)
})

来源:https://stackoverflow.com/questions/60159576/is-it-possible-to-read-from-multiple-child-nodes

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