Pankaj99
Pankaj99

Reputation: 13

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

I want to fetch data from team of subscription collection.

enter image description here

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

enter image description here

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

Upvotes: 1

Views: 7473

Answers (1)

TheTiger
TheTiger

Reputation: 13364

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

Upvotes: 1

Related Questions