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

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

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.

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)]
             }       
    }
    // Place where I need the array result
}
// 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.

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

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

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

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
    // 在这里进行比较
}
英文:

You can't make an asynchronous call synchronous you need

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 
}  

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

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定