I am trying to fetch data from Firestore database in my ios Swift Project

。_饼干妹妹 提交于 2020-08-26 06:47:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!