检索firestore引用

时间:2018-03-04 06:22:38

标签: android firebase google-cloud-firestore

我是Android和Firestore的新手,我能够毫无问题地从Firestore检索数据 但是,我不确定如何使用该参考。

这是<div class="input-group plus-minus-input"> <div class="input-group-button"> <button class="button hollow circle1 btn btn-primary" data-field="quantity" data-quantity="minus" type="button"><i aria-hidden="true" class="fa fa-minus"></i></button> </div><input class="input-group-field" name="quantity" type="number" value="0"> <div class="input-group-button"> <button class="button hollow circle2 btn btn-primary" data-field="quantity" data-quantity="plus" type="button"><i aria-hidden="true" class="fa fa-plus"></i></button> </div> </div> <button class="btn btn-danger" type="button">Add to cart</button> ;

的数据结构
person

所以我得到像这样的useraccount-reference;

{ name=abc, gender=male, title=software engineer, company=abc, useraccount=com.google.firebase.firestore.DocumentReference@4878aac   
}

我的问题是如何使用此userAccountRef从DocumentReference userAccountRef = doc.getDocumentReference("useraccount"); 检索信息?我没有看到任何API来通过引用获取文档。

1 个答案:

答案 0 :(得分:0)

要从DocumentReference获取文档,请在其上调用get()。来自Firebase documentation

userAccountRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null && document.exists()) {
                Log.d(TAG, "DocumentSnapshot data: " + document.getData());
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});