我正在编写一个函数,该函数从API调用中提取信息,解析生成的JSON,遍历每个结果项并显示此信息以下视图控制器。在这个循环中,我必须从我们的Firestore数据库中获取每个项目的信息。Cloud Firestore - 利用循环getDocument请求(500+文档)(Swift)缓慢执行
我用斯威夫特4 &公司的FireStore 0.8.0
下面的代码是我一直使用的是什么,到目前为止,它考虑到如果一个项目可能无法在我们的数据库中,有Segue公司仅发生在所有公司的FireStore请求已经完成:
for item in results {
dispatch.enter()
//Main loop code for processing the API Call & pulling the document ID for this item
let docRef = db.collection("collection").document(documentID)
docRef.getDocument { (document, error) in
if (document?.exists ?? false), error == nil {
if let document = document {
let data = document.data()
print("EXISTS")
//do things
dispatch.leave()
} else {
print("Document does not exist")
dispatch.leave()
}
} else {
print("DOES NOT EXIST")
//do other things
dispatch.leave()
}
}
}
dispatch.notify(queue: .main) {
//perform a segue to the data display VC
}
(代码是用这个问题写 - >Crashes with Firestore,因此目前公司的FireStore最初不返回零值,如果您的集合中不存在的文件)
我遇到的问题是,此功能最终需要花费几分钟才能完成。有没有更快的方式来执行这个循环的文档请求?这仅仅是测试版性能吗?
我们公司的FireStore采集最终将有超过100多万的文件,并从每个文件拉到字段是一个嵌套的起源如下:
collection {
document {
object {
item1: data to pull
item2: data to pull
...etc
}
}
}
任何帮助将不胜感激!
UPDATE &结论
好像我正在此相反的方法!由于我们数据库的性质意味着我们存储了API数据的所有文档,因此我可以在Firestore数据库上执行单个查询来限制数据,然后强制API调用仅基于初始查询找到的文档返回结果。这消除了检查数据库中是否存在任何API调用结果文档的需求!十分简单!
是否有理由不能加载整个集合而不是一次循环并加载一个文档? –
@MorganChen如果您指的是getDocuments,那么在用户设备上的性能会很高。他们会加载1,000,000个大小为50kb的文档,以便显示每个文档中可能有5kb大小的数据,大约有500-1,000个结果 – Hendies
我可能会误解,但是您是否有理由限制查询以仅加载这些文档500-1000的结果反而加载全部1000000? –