基于返回的queryEqual(toValue)填充tableview

时间:2017-08-22 01:37:22

标签: swift firebase firebase-realtime-database parent-child children

我无法根据快照键填充tableView。控制台正在打印父节点的所有(nodeToReturn)值和从queryEqual(toValue:locationString)调用的子节点,所以我知道我正在查询它们。但由于某种原因,我的tableView不断填充我的Database.database()。reference()。child(“Travel_Experience_Places”)中的所有用户词典。我只是希望tableView显示来自“nodeToReturn”值的快照数据,因此不是来自我的“Travel_Experience_Places”数据库引用的每个父节点 - 只有在其子节点中具有相同“locationString”值的父节点。希望这是有道理的。提前谢谢!

//模型对象

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结构

  "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在tableview中返回firebase queryEqual(toValue:locationString)

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
}

1 个答案:

答案 0 :(得分:1)

我相信你需要在你获得nodeToReurn的块中重新加载tableview

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