我正在尝试从ios Swift项目中的Firestore数据库中获取数据 [英] I am trying to fetch data from Firestore database in my ios Swift Project
本文介绍了我正在尝试从ios Swift项目中的Firestore数据库中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从订阅集合团队中获取数据.
I want to fetch data from team of subscription collection.
我正在尝试以下代码:
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())
}
}
我的代码输出
因此,就我而言,我能够获取馆藏数据,但无法从团队那里获取数据,请帮帮我,谢谢支持
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
推荐答案
如果您不想监听每个事件,请使用 getDocuments
方法而不是 addSnapshotListener
.
If you don't want to listen every event use getDocuments
method instead of addSnapshotListener
.
/// This will give you team data
document.data()["team"]
从firestore获取 team
信息后.这是获取 name
和 officeId
的方法:
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)")
}
}
输出
name: Developer Ratufa, officeId: myuPlTBO8sEM4SOQ8rWY
name: , officeId: myuPlTBO8sEM4SOQ8rWY
这篇关于我正在尝试从ios Swift项目中的Firestore数据库中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文