Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重复相同的查询快照,带来相同的数据,尽管firestore中添加了新数据_Ios_Swift_Google Cloud Firestore - Fatal编程技术网

Ios 重复相同的查询快照,带来相同的数据,尽管firestore中添加了新数据

Ios 重复相同的查询快照,带来相同的数据,尽管firestore中添加了新数据,ios,swift,google-cloud-firestore,Ios,Swift,Google Cloud Firestore,我一直在按照文档在我的数据库中创建查询搜索,以获取要在tableView中显示的最高分 下面的代码可以工作,但是,当添加新的(更高分)时,tableview仍然显示旧的查询数据 我的数据库里有三个最高分。。[121, 131, 134]. 在第一次查询后,这些信息将正确显示 然后我继续玩游戏并添加更多分数[121、131、134、234、432],但相同的结果显示在我的tableView[121、131、134]中 我将我的显示限制为三个项目。所以它真的应该显示。。。。。[134234432]

我一直在按照文档在我的数据库中创建查询搜索,以获取要在tableView中显示的最高分

下面的代码可以工作,但是,当添加新的(更高分)时,tableview仍然显示旧的查询数据

我的数据库里有三个最高分。。[121, 131, 134]. 在第一次查询后,这些信息将正确显示

然后我继续玩游戏并添加更多分数[121、131、134、234、432],但相同的结果显示在我的tableView[121、131、134]中

我将我的显示限制为三个项目。所以它真的应该显示。。。。。[134234432]

我试着用鼠标清除我的桌面视图

    scoresArray.removeAll()
    topScoresTableView.reloadData()
(我离开VC时称之为)但这没有效果

class ScoreClass {

let db = Firestore.firestore()


var name = ""
var score = 0

init(withName: String, andScore: Int) {
    name = withName
    score = andScore
}
}

let ref = Database.database().reference()

func retrieveUserData() {

    let postsRef = self.db.collection("users")

    let query = postsRef
                    .whereField("highScore", isGreaterThan: 5000)
                    .order(by: "highScore", descending: false)
                    .limit(to: 3)

    query.getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    print("\(document.documentID) => \(document.data())")
                    let dict = document.data()
                    let name = dict["username"] as! String
                    let score = dict["highScore"] as! Int
                    let aScore = ScoreClass(withName: name, andScore: score)
                    self.scoresArray.insert(aScore, at: 0)
                }
            self.topScoresTableView.reloadData()
            }
    }
}


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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = topScoresTableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath) as! TableViewCell
    let row = indexPath.row
    let scoreClassObject = scoresArray[row]
    let name = scoreClassObject.name
    let score = scoreClassObject.score
    cell.backgroundColor = UIColor.clear
    cell.usernameLabel.text = name
    cell.resultLabel.text = String(score)
    cell.rankNumberLabel.text = "\(indexPath.row + 1)"
    print(scoresArray)
    return cell
}

您的代码未安排从Firestore接收实时更新。只需使用查询Firestore一次

相反,您需要做的是将代码更改为using,然后在每次调用侦听器时,当查询结果随时间变化时,更新TableView