swift - populate tableview based on returned queryEqual(toValue) -
i having trouble populating tableview based on snapshot keys. console printing (nodetoreturn) values parent node , children called queryequal(tovalue: locationstring), know querying them correctly. reason tableview keeps populating user dictionaries database.database().reference().child("travel_experience_places"). want tableview display snapshot data "nodetoreturn" values, not every parent node "travel_experience_places" database reference - parent nodes have same value of "locationstring" in children. makes sense. 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 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 , child values have locationstring child value in 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 }
i believe need reload tableview in block 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) */ }
wiki
Comments
Post a Comment