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

穿精又带淫゛_ 提交于 2020-01-16 08:33:08

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/58297645/what-is-the-use-case-of-reference-type-in-firestore-document-and-how-to-use-it-s

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