0

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

Image of database:

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)
})

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    I doubt that your first query doesn't retrieve anything. It should get all child nodes under "posts". You're probably just not handling the query result snapshot correctly. – Doug Stevenson Feb 10 '20 at 22:52
  • It does end up getting "2" snapshots, but will it be able to read the data? – altautah801 Feb 10 '20 at 23:00
  • 1
    Those snapshots contain all the data. You probably didn't dig into them enough. – Doug Stevenson Feb 10 '20 at 23:13
  • If you attach a listener to `posts`, you'll get a snapshot with all data under `posts`. If you're having problems processing that data correctly, edit your question to include the minimal code that reproduces that problem. One thing to keep in mind: it's often easier to help if you just `print` the data, and don't focus on UI aspects. Also see [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). – Frank van Puffelen Feb 11 '20 at 00:49
  • I've added the listener code to the question. Problem: it will only work with CODE B and I can't read anything from "Beauty & Style" – altautah801 Feb 11 '20 at 01:43
  • I'm not sure what you expect `return completion(tempPosts)` to do, but you can't return something out of the closure/callback. If you want to execute some different code with the results of loading the data, you will have to call that code from within the closure. See https://stackoverflow.com/a/31316513, https://stackoverflow.com/a/42464431, https://stackoverflow.com/a/41797783, https://stackoverflow.com/a/38591479 – Frank van Puffelen Feb 11 '20 at 04:59

0 Answers0