user3708224
user3708224

Reputation: 1259

populate tableview based on returned queryEqual(toValue)

I am having trouble populating a tableView based on snapshot keys. The console is printing all (nodeToReturn) values from the parent node and children that are called from the queryEqual(toValue: locationString), so I know I am querying them correctly. But for some reason my tableView keeps populating all the User dictionaries from my Database.database().reference().child("Travel_Experience_Places"). I just want the tableView to display the snapshot data from the "nodeToReturn" values, so not every parent node from my "Travel_Experience_Places" database reference - only the parent nodes that have the same value of "locationString" in its children. Hopefully that makes sense. Thanks in advance!

// model object

class User: SafeUserObject {

    var id: String?
    var name: String?
    var email: String?
    var profileImageUrl: String?
    var place: String?

    init(dictionary: [String: AnyObject]) {
        super.init()

        self.id = dictionary["fromId"] as? String
        self.name = dictionary["addedBy"] as? String
        self.email = dictionary["email"] as? String
        self.profileImageUrl = dictionary["profileImageUrl"] as? String
        self.place = dictionary["place"] as? String
        setValuesForKeys(dictionary)
    }
}

// JSON structure

  "Travel_Experience_Places" : {
    "-Ks0Ms4fEQZxBytI-vu_" : {
      "addedBy" : "Daniel Meier",
      "place" : "Barcelona, Spain",
      "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/travelapp-255da.appspot.com/o/profile_images%2F21673E85-4F58-480D-B0A9-65884CADF7B3.png?alt=media&token=9e111014-d1d7-4b3b-a8c2-3897032c34cc",
      "timestamp" : 1.503261589872372E9
    },

// tableview to return firebase queryEqual(toValue: locationString) in tableview

var experiencePlaceUsers = [User]()

    func fetchTravelingUserCell() {

        let databaseRef = Database.database().reference().child("Travel_Experience_Places")
        databaseRef.queryOrdered(byChild: "place").queryEqual(toValue: locationString).observe(.value, with: { snapshot in

            if snapshot.exists(){
                let allKeys = snapshot.value as! [String : AnyObject]
                let nodeToReturn = allKeys.keys
                print(nodeToReturn) // prints parent and child values that have locationString as a child value in its data structure
            }
        })

        databaseRef.observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                let user = User(dictionary: dictionary)
                self.experiencePlaceUsers.append(user)

                self.tableView.reloadData()
            }
        }, withCancel: nil)
    }

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return experiencePlaceUsers.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell
    cell.user = experiencePlaceUsers[indexPath.item]

    return cell
}

Upvotes: 2

Views: 70

Answers (1)

3stud1ant3
3stud1ant3

Reputation: 3606

I believe you need to reload the tableview in your block where you get nodeToReurn

var experiencePlaceUsers = [User]()

    func fetchTravelingUserCell() {



    let databaseRef = Database.database().reference().child("Travel_Experience_Places")
                databaseRef.queryOrdered(byChild: "place").queryEqual(toValue: locationString).observe(. childAdded, with: { snapshot in



                if snapshot.exists(){
                    if let allKeys = snapshot.value as? [String: AnyObject] {
                        let singleUser = User(dictionary: allKeys)
                        self.experiencePlaceUsers.append(singleUser)


                        DispatchQueue.main.async(execute: {
                           self.tableView.reloadData()
                        })
                   }
                }
            }) 


   /*
        databaseRef.observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                let user = User(dictionary: dictionary)
                self.experiencePlaceUsers.append(user)

                self.tableView.reloadData()
            }
        }, withCancel: nil)
*/
    }

Upvotes: 1

Related Questions