Ssubrat Rrudra
Ssubrat Rrudra

Reputation: 960

What is the use case of reference type in Firestore Document and how to use it specially in an android app?

In my Android app, if I get the document path by getting the value of reference type field of a document then how am I going to call it? Because the reference path I'm going to get would be something like /Users/OctUsers/1stWeekUsers/OlCvJFfWZeAlcttdlgzz/ and method for retrieving a Firestore document is something like FirebaseFirestore.getInstance().collection(collectionPath).document(documentPath).collection(collectionPath).document(documentPath) and so on. How am I going to iterate over reference path for getting every collectionPath and documentPath to be used in the Firestore DocumentReferece#get method?

Has anyone actually used this reference field type in their Firestore database then please drop the screenshot for letting us know more use cases of this?

enter image description here

Upvotes: 1

Views: 2439

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317467

When you query for a document contains a reference type field, the field will show up in a DocumentSnapshot on an Android client as a DocumentReference type object. You can treat this just like any other DocumentReference that you build on your own. You can call get() on it to get the referred document a you would normally. You don't have to do anything to parse the path to the document (unless you want to).

Upvotes: 2

Alex Mamo
Alex Mamo

Reputation: 138824

If you are storing the following document reference in the database:

/Users/OctUsers/1stWeekUsers/OlCvJFfWZeAlcttdlgzz/

And you want to get this:

FirebaseFirestore.getInstance().collection(collectionPath).document(documentPath).collection(collectionPath).document(documentPath)

You can symply rewrite it like this:

FirebaseFirestore.getInstance().document("/Users/OctUsers/1stWeekUsers/OlCvJFfWZeAlcttdlgzz/");

Remember, the most important part in a DocumenetReference object is the string path. If you want to get that String representation, use DocumentReference's getPath() method for that.

Furthermore, if you need to deserialize that path String back into a DocumentReference object, simply use FirebaseFirestore.getInstance().document(path).

Upvotes: 5

Related Questions