为什么我无法将 Firestore 中获取的值分配给 Swift 中的数组? [英] Why I couldn't assign fetched values from Firestore to an array in Swift?

查看:11
本文介绍了为什么我无法将 Firestore 中获取的值分配给 Swift 中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了多次使用各种方法将收集到的来自 firestore 的文档值分配到数组中.不幸的是,我找不到解决这个问题的方法.我附上了我最近尝试实现的代码.它在 Firestore 关闭之前包含一个打印语句,该语句成功打印整个获取的值.但是,在关闭之后,我尝试打印相同的数组,结果是一个空数组.

I tried several times with various ways to assign the collected values of documents from firestore into an array. Unfortunately, I could't find a way to solve this issue. I attached the code that I recently tried to implement. It includes before Firestore closure a print statement which print the whole fetched values successfully. However, after the closure and I tried to print the same array and the result is an empty array.

我试图实现这段代码

var hotelCities: [String] = []

func getCities() {
    db.collection("Hotels").getDocuments() { (querySnapshot,  err) in
        if let err = err {
            print("Error getting documents: (err)")
        } else {
            for document in querySnapshot!.documents {
                var found = false
                let documentDetails = document.data() as NSDictionary
                let location = documentDetails["Location"] as! NSDictionary
                let city = location["city"]!
                if (self.hotelCities.count == 0) {
                    self.hotelCities.append(String(describing: city))
                }
                else{
                    for item in self.hotelCities {
                        if item == String(describing: city){
                            found = true
                        }
                    }
                    if (found == false){
                        self.hotelCities.append(String(describing: city))
                    }
                }
            }
        }
        print(self.hotelCities)
    }
    print(self.hotelCities)
}

推荐答案

这实际上是预期的结果,因为数据是从 Firestore 异步加载的.

That's actually the expected result, since data is loaded from Firestore asynchronously.

一旦您调用 getDocuments(),Firestore 客户端就会连接到服务器以读取这些文档.由于这可能需要相当长的时间,因此它允许您的应用程序在此期间继续运行.然后,当文档可用时,它会调用您的关闭.但这意味着文档只有在调用闭包后才可用.

Once you call getDocuments(), the Firestore client goes of and connects to the server to read those documents. Since that may take quite some time, it allows your application to continue running in the meantime. Then when the documents are available, it calls your closure. But that means the documents are only available after the closure has been called.

通过放置一些打印语句,最容易理解此流程:

It's easiest to understand this flow, by placing a few print statements:

print("Before starting to get documents");
db.collection("Hotels").getDocuments() { (querySnapshot,  err) in
  print("Got documents");
}
print("After starting to get documents");

当你运行这段代码时,它会打印:

When you run this code, it will print:

在开始获取文件之前

开始获取文件后

有文件

现在,当您第一次看到此代码时,这可能不是您期望的输出.但这完全解释了为什么您在关闭后的 print(self.hotelCities) 不打印任何内容:数据尚未加载.

Now when you first saw this code, that is probably not the output your expected. But it completely explains why the print(self.hotelCities) you have after the closure doesn't print anything: the data hasn't been loaded yet.

快速的解决方案是确保所有需要文档的代码都在加载文档时调用的 close 内.就像你最上面的 print(self.hotelCities) 语句一样.

The quick solution is to make sure that all code that needs the documents is inside of the close that is called when the documents are loaded. Just like your top print(self.hotelCities) statement already is.

另一种方法是定义您自己的闭包,如以下答案所示:https://stackoverflow.com/a/38364861

An alternative is to define your own closure as shown in this answer: https://stackoverflow.com/a/38364861

这篇关于为什么我无法将 Firestore 中获取的值分配给 Swift 中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆