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

huangapple go评论112阅读模式
英文:

How to handle Firestore Query Results for later usage

问题

I am currently struggling with my Firestore query results.
What I want to do is to query the database, fetch the results, write the result in an array --> this is already working with my code below!
But in addition to this I need that array later to compare it to a different array from a second query and this is where the issue is.

  1. var arrayOfTuples = [(Int, String)]()
  2. db.collection("Data").whereField("age", isGreaterThanOrEqualTo: 1).whereField("age", isLessThanOrEqualTo: 50).whereField("gender", isEqualTo: "F").getDocuments() { (querySnapshot, err) in
  3. if let err = err {
  4. print("Error getting documents: \(err)")
  5. } else {
  6. for (index, document) in querySnapshot!.documents.enumerated() {
  7. arrayOfTuples += [(index, document.documentID)]
  8. }
  9. }
  10. // Place where I need the array result
  11. }
  12. // Continue with your code here

I can use the array result in the last print method, but I need it out of the last bracket that I can compare it to the second query result.
Have not found anything that I could return the result, but maybe it's completely simple (hope so).

Hope it's clear, otherwise just let me know.
Thanks!

英文:

I am currently struggling with my Firestore query results.
What I want to do is to query the database, fetch the results, write the result in an array --> this is already working with my code below!
But in addition to this I need that array later to compare it to a different array from a second query and this is where the issue is.

  1. var arrayOfTuples = [(Int, String)]()
  2. db.collection("Data").whereField("age", isGreaterThanOrEqualTo: 1).whereField("age", isLessThanOrEqualTo: 50).whereField("gender", isEqualTo: "F").getDocuments() { (querySnapshot, err) in
  3. if let err = err {
  4. print("Error getting documents: \(err)")
  5. } else {
  6. for (index, document) in querySnapshot!.documents.enumerated() {
  7. arrayOfTuples += [(index, document.documentID)]
  8. }
  9. }
  10. print(arrayOfTuples)
  11. }
  12. //Place where I need the array result

I can use the array result in the last print method, but I need it out of the last bracket that I can compare it to the second query result.
Have not found anything that I could return the result, but maybe it's completely simple (hope so).

Hope it's clear, otherwise just let me know.
Thanks!

答案1

得分: 2

你无法将异步调用变成同步,你需要

  1. func loadQuery(com: @escaping ([(Int, String)] -> ())) {
  2. var arrayOfTuples = [(Int, String)]()
  3. db.collection("Data")
  4. .whereField("age", isGreaterThanOrEqualTo: 1)
  5. .whereField("age", isLessThanOrEqualTo: 50)
  6. .whereField("gender", isEqualTo: "F")
  7. .getDocuments() { (querySnapshot, err) in
  8. if let err = err {
  9. print("Error getting documents: \(err)")
  10. } else {
  11. for (index, document) in querySnapshot!.documents.enumerated() {
  12. arrayOfTuples += [(index, document.documentID)]
  13. }
  14. }
  15. com(arrayOfTuples)
  16. }
  17. }
  18. loadQuery { arr in
  19. // 在这里进行比较
  20. }
英文:

You can't make an asynchronous call synchronous you need

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

  1. loadQuery { arr in
  2. // compare here
  3. }

huangapple
  • 本文由 发表于 2020年1月3日 23:32:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581234.html
  • arrays
  • google-cloud-firestore
  • ios
  • swift

在Nunjucks模板中访问数组在 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定