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

查看:51
本文介绍了为什么我无法从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:

开始获取文档之前

Before starting to get documents

开始获取文件后

获得的文件

现在,当您第一次看到此代码时,可能不是您期望的输出.但这完全解释了为什么闭包后的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.

快速的解决方案是确保需要文档的所有代码都在装入文档时调用的关闭内.就像您最重要的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天全站免登陆