I'm coding a function that pulls information from an API call, parses the resulting JSON, loops through each result item and then displays this information on the following View Controller. Within this loop, I have to pull information for each item from our Firestore database.
I'm using Swift 4 & Firestore 0.8.0
The code below is what I've been using so far, which takes into account if an item might not exist on our database, with a segue occurring only when all Firestore requests have been completed:
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
}
(The code was written using this question -> Crashes with Firestore, whereby currently Firestore doesn't initially return nil values if documents don't exist in your collection)
The problem I have is that this function ends up taking several minutes to complete. Is there a faster way of performing this looped document request? Is this just beta performance?
Our Firestore collection will eventually have over 1,000,000 documents, and the fields that are pulled from each document are of a nested origin as follows:
collection {
document {
object {
item1: data to pull
item2: data to pull
...etc
}
}
}
Any help would be greatly appreciated!
UPDATE & CONCLUSION
Seems like I was taking the reverse approach to this! Since the nature of our database means that we store all documents of the API data, I can perform a single query on our Firestore database to limit data before forcing the API call to only return results based on documents found by the initial query. This removes the need to check if any API call result documents are present in our database! Easy-peasy!