[英]Firebase Cloud Firestore follow reference
I've gotten a queryDocumentSnapshot
with the following code: 我得到了带有以下代码的queryDocumentSnapshot
:
mFireStore = FirbaseFirestore.getInstance();
mFireStore.collection("myCollection").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
// Set some data from the doc.getDocument here
mFireStore.collection("myCollection").document(doc.getDocument().getId()).collection("subCollection").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (queryDocumentSnapshots.isEmpty()){
// not found
} else{
List<DocumentSnapshot> hosts = queryDocumentSnapshots.getDocuments();
for (int i = 0; i < hosts.size(); i++){
// Each hosts DocumentSnapshot has a "host" field which contains a reference to a document
// I'd like to follow this reference and get that document
}
}
}
});
My issue is that once I've gotten the list of host DocumentSnapshots
, I'd like to follow their "host" field which contains a reference to a document. 我的问题是,一旦获得了宿主DocumentSnapshots
的列表,我想跟随它们的“宿主”字段,其中包含对文档的引用。 I would like to follow this reference and get the document at the other end but do not know how. 我想遵循此参考资料并获得另一端的文档,但不知道如何操作。 If you notice any other problems with my logic, feel free to point it out as I am very new to Cloud Firestore. 如果您发现我的逻辑有任何其他问题,请随时指出,因为我是Cloud Firestore的新手。
To solve this, you can simply iterate over the hosts
list. 为了解决这个问题,您可以简单地遍历hosts
列表。 If your host
field holds the reference as a String
, then please use the following lines of code: 如果您的host
字段将引用保存为String
,那么请使用以下代码行:
for (DocumentSnapshot ds : hosts) {
String host = ds.getString("host");
Log.d("TAG", host);
}
If your host
field holds the reference as a DocumentReference
, then please use the following line of code: 如果您的host
字段将引用保存为DocumentReference
,那么请使用以下代码行:
DocumentReference host = ds.getDocumentReference("host");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.