英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论