简体   繁体   English

如何使用 Firebase Firestore 获取查询数据?

[英]How to get query data using Firebase Firestore?

Query query = mTournamentColRef.whereEqualTo("author", user.getUid());

I want to get the id of the Document inside mTournamentColRef , but could not find anything in the Firebase documentation talking about what to do with the query and how to get its data.我想在mTournamentColRef获取文档的 ID,但在Firebase 文档中找不到任何关于如何处理查询以及如何获取其数据的内容。

Thank you for your help!感谢您的帮助!

You can use data snapshot to get data from query or if you are using Firestore Recycler Adapter you can directly pass the query to the constructor.您可以使用数据快照从查询中获取数据,或者如果您使用 Firestore Recycler Adapter,您可以直接将查询传递给构造函数。 Here is the code to get specific data:-这是获取特定数据的代码:-

Query query = mTournamentColRef.whereEqualTo("author", user.getUid());
            query.addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e)
                {
                    for (DocumentSnapshot doc : queryDocumentSnapshots)
                    {
                        Tournament tp = doc.toObject(Tournament.class);
                    }
                }
            });

How to get query data using firebase firestore?如何使用firebase firestore获取查询数据?

Simply, use a get() call!简单地说,使用get()调用!

query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Log.d("TAG", "Document Id: " + document.getId());
            }
        }
    }
});

There is no need to use addSnapshotListener unless you need to get the data in real-time.除非需要实时获取数据,否则无需使用addSnapshotListener If you only need to get it and not listen for changes, just use the get() method.如果您只需要获取它而不监听更改,只需使用get()方法。

你可以通过 dicumentsnapshot.getId() 获得我想要的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM