问题
I want to fetch data from team of subscription collection.
I am trying following code:
db.collection("subscriptions").addSnapshotListener { querySnapshot, error in
guard let snapshot = querySnapshot else {
print("Error retreiving snapshots \(error!)")
return
}
//print("Current data: \(snapshot.documents.map { $0.data() })")
for document in snapshot.documents{
print(document.data())
}
}
Output of my Code
So, as far concern i am able to fetch the data of collections but not able to get data from team , help me out , Thanx for support
回答1:
If you don't want to listen every event use getDocuments method instead of addSnapshotListener.
/// This will give you team data
document.data()["team"]
After getting the team information from firestore. Here is how to get name and officeId:
if let teamInfo = document.data()["team"] as? [String: Any] {
let teams = teamInfo.map {$0.value}
for team in teams {
guard let validTeam = team as? Dictionary<String, Any> else {continue}
let name = validTeam["name"] as? String ?? ""
let officeId = validTeam["officeId"] as? String ?? ""
print("name: \(name), officeId: \(officeId)")
}
}
Output
name: Developer Ratufa, officeId: myuPlTBO8sEM4SOQ8rWY
name: , officeId: myuPlTBO8sEM4SOQ8rWY
来源:https://stackoverflow.com/questions/50525462/i-am-trying-to-fetch-data-from-firestore-database-in-my-ios-swift-project