ios - 如何处理 Firestore 查询结果供以后使用

标签 ios arrays swift google-cloud-firestore

我目前正在努力处理我的 Firestore 查询结果。 我想做的是查询数据库,获取结果,将结果写入数组 --> 这已经在我下面的代码中使用了! 但除此之外,稍后我还需要该数组将其与第二个查询中的另一个数组进行比较,这就是问题所在。

var arrayOfTuples = [(Int, String)]()

db.collection("Data").whereField("age", isGreaterThanOrEqualTo: 1).whereField("age", isLessThanOrEqualTo: 50).whereField("gender", isEqualTo: "F").getDocuments() { (querySnapshot, err) in
    if let err = err {
        print("Error getting documents: \(err)")
    } else {
             for (index, document) in querySnapshot!.documents.enumerated() {
             arrayOfTuples += [(index, document.documentID)]
             }       
    }
    print(arrayOfTuples)
} 
//Place where I need the array result

我可以在最后一个打印方法中使用数组结果,但我需要它在最后一个括号之外,以便我可以将它与第二个查询结果进行比较。 还没有找到任何我可以返回结果的东西,但也许它很简单(希望如此)。

希望它是清楚的,否则请告诉我。 谢谢!

最佳答案

你不能让你需要的异步调用同步

func loadQuery(com:@escaping( [(Int, String)] -> ())){ 
    var arrayOfTuples = [(Int, String)]() 
    db.collection("Data").whereField("age", isGreaterThanOrEqualTo: 1).whereField("age", isLessThanOrEqualTo: 50).whereField("gender", isEqualTo: "F").getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
                 for (index, document) in querySnapshot!.documents.enumerated() {
                 arrayOfTuples += [(index, document.documentID)]
                 }       
        }
        com(arrayOfTuples)
    } 
}

loadQuery { arr in 
  // compare here 
}  

关于ios - 如何处理 Firestore 查询结果供以后使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59581234/

相关文章:

ios - 应用程序在 gpus_ReturnNotPermittedKillClient 崩溃

ios - 增加内容大小时如何防止内容偏移

ios - 以编程方式快速创建 uitableview 而不是加载

javascript - 访问另一个数组内部的数组以使用 v-for

ios - 如何在 Swift 中使用协议(protocol)初始化 UIViews 和 UIViewControllers

ios - 替换什么[读者解雇ModalViewControllerAnimated :NO with?

PHP array_unique 转换为对象

c - 在c中按螺旋顺序填充二维数组

ios - 手动添加要查看的项目后无法滚动到 UIScrollView 的底部

swift - 仅在 swift 中显示 WebKitWebView 的特定部分