[ad_1]
I’m new to SwiftUI and Firebase and I’m attempting to construct my first app. I’m storing Recreation paperwork in Firestore and one of many fields is an array containing the consumer ids of the gamers as you may see within the picture.
Recreation knowledge construction
That being stated, I’m attempting to listing all video games of a given consumer and have all of the gamers listed in every one of many cells (the order is necessary).
With a view to create the listing of video games within the UI I created a GameCellListView and a GameCellViewModel. The GameCellViewModel ought to load each the video games and the array of customers that correspond to the gamers of every sport. Nevertheless I’m not having the ability to load the customers to an array. I’ve to undergo the gamers array and question the database for every Id and append to a Person array; then I ought to have the ability to return this Person array. Since I am utilizing a for loop, I am unable to assign the values to the array after which return it. I attempted utilizing map(), however I am unable to carry out a question within it.
The objective is to load that “all” var with a struct that receives a sport and its gamers GamePlayers(gamers: [User], sport: Recreation)
It ought to look one thing just like the code snippet under, however the customers array at all times comes empty. This operate runs on GameCellViewModel init.
I hope you may perceive my drawback and thanks prematurely! Been caught on this for two weeks now
func loadData() {
let userId = Auth.auth().currentUser?.uid
db.assortment("video games")
.order(by: "createdTime")
.whereField("userId", isEqualTo: userId)
.addSnapshotListener { (querySnapshot, error) in
if let querySnapshot = querySnapshot {
self.video games = querySnapshot.paperwork.compactMap { doc in
do {
let extractedGame = attempt doc.knowledge(as: Recreation.self)
var consumer = [User]()
let customers = extractedGame!.gamers.map { playerId -> [User] in
self.db.assortment("customers")
.whereField("uid", isEqualTo: playerId)
.addSnapshotListener { (querySnapshot, error) in
guard let paperwork = querySnapshot?.paperwork else {
print("No paperwork")
return
}
consumer = paperwork.compactMap { queryDocumentSnapshot -> Person? in
return attempt? queryDocumentSnapshot.knowledge(as: Person.self)
}
}
return consumer
}
self.all.append(GamePlayers(gamers: customers.first ?? [User](), sport: extractedGame!))
return extractedGame
}
catch {
print(error)
}
return nil
}
}
}
}
[ad_2]