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

Ssubrat Rrudra :

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

Alex Mamo :

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).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=309952&siteId=1