I have an Array where some different UserID's are stored. Each UserID is connected with their corresponding data in Firestore. So now I want to fetch all the JSON Data from the UserID's and make them accessible to decode them later. Therefore the fetched Data (coming as a Dictionary) from each user must be accessible separately. I tried it with that way:
var fetchedIDs = ["ID1", "ID2", "ID3"]
var finalArray = [[String: Any]]()
for id in fetchedIDs{
let docRef = Firestore.firestore().collection("Data").document(id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let myDict: [String: Any] = document.data() as! [String: Any]
finalArray.append(myDict)
}
}
}
}
But the problem is that the finalArray (like finalArray[0]) is just accessible in the For-Loop.
It should be accessible outside of the loop and look like that:
finalArray[0] should have the Dictionary data from ID1
finalArray[1] should have the Dictionary data from ID2
Maybe I am thinking to complicated for that..
Can someone help me out ?
Is there generally a good source for learning about scopes and how the data should be accessed like in that scenario? Thanks!