Mike
Mike

Reputation: 1331

How do I access the result of a returned query in firestore (swift)?

I've got a little chat application and i just want to query who i'm chatting with.

I read the firestore docs at https://firebase.google.com/docs/firestore/query-data/queries

And there they show a few examples like:

let query = citiesRef.whereField("state", isEqualTo: "CA")

In my application my query is:

let query = model.defaultStore?.collection("users").document(myid).collection("chats").whereField("chatWith", isEqualTo: userII)

and i would like the id of the document that fulfils this query.

Just not sure how to get it.

Thanks.

Upvotes: 3

Views: 2349

Answers (1)

Paulo
Paulo

Reputation: 602

I’m not at a computer at the moment, however, after a quick google, I believe this is the sort of thing you are after:

db.collection("users").document(myid).collection("chats").whereField("chatWith", isEqualTo: userII).getDocuments() { (querySnapshot, err) in
    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        for document in querySnapshot!.documents {
            print("\(document.documentID) => \(document.data())")
        }
    }
}

You basically just add getDocuments() to the end of your query, and a closure to handle the results.

Hope that helps

Upvotes: 3

Related Questions