Android Google Firestore - 监听多个文档
创始人
2024-08-13 23:00:14
0

您可以使用Firestore的addSnapshotListener方法来监听多个文档。下面是一个示例代码:

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference collectionRef = db.collection("your_collection");

collectionRef.document("doc1").addSnapshotListener(new EventListener() {
    @Override
    public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "Listen failed.", e);
            return;
        }

        if (documentSnapshot != null && documentSnapshot.exists()) {
            Log.d(TAG, "Document 1 data: " + documentSnapshot.getData());
        } else {
            Log.d(TAG, "Document 1 does not exist");
        }
    }
});

collectionRef.document("doc2").addSnapshotListener(new EventListener() {
    @Override
    public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "Listen failed.", e);
            return;
        }

        if (documentSnapshot != null && documentSnapshot.exists()) {
            Log.d(TAG, "Document 2 data: " + documentSnapshot.getData());
        } else {
            Log.d(TAG, "Document 2 does not exist");
        }
    }
});

在这个例子中,我们首先获取到Firestore实例,然后使用collection方法获取到一个指定的集合。然后我们使用document方法指定要监听的文档。在每个addSnapshotListener方法中,我们可以实现一个EventListener来处理对应文档的更改。在监听器中,您可以根据需要进行适当的操作。

请注意,这个示例代码中的TAG是一个String变量,您可以根据自己的需要定义它。还要确保您已经在您的项目中添加了Firestore的依赖。

相关内容

热门资讯

总结辅助挂!德州透视竞技联盟(... 总结辅助挂!德州透视竞技联盟(透视)微乐小程序辅助器脚本(好像是有辅助app)-哔哩哔哩1、实时德州...
透视攻略!德州局透视脚本(透视... 透视攻略!德州局透视脚本(透视)微乐小程序黑科技辅助工具2026最新推荐(一直有辅助app)-哔哩哔...
透视数据!wepoker作必弊... 透视数据!wepoker作必弊视频(透视)贵州微乐自建房辅助软件下载(总是存在有辅助软件)-哔哩哔哩...
2026版方法!wepoker... 2026版方法!wepoker破解版内购(透视)微乐小程序辅助工具2025(其实存在有辅助教程)-哔...
2026版技巧!hhpoker... 2026版技巧!hhpoker真能买到挂吗(透视)微乐手游辅助脚本平台(本来存在有辅助app)-哔哩...
此事迅速冲上热搜!hhpoke... 此事迅速冲上热搜!hhpoker外开挂靠谱吗(透视)微乐广西小程序(一贯真的有辅助工具)-哔哩哔哩1...
透视策略!wpk透视辅助方法(... 透视策略!wpk透视辅助方法(德普之星破解版)果然真的有辅助app(哔哩哔哩)1、用户打开应用后不用...
透视辅助!pokerworld... 您好,pokerworld破解版下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275...
透视办法!wpk辅助(WePo... 透视办法!wpk辅助(WePoKer功能)一直有辅助软件(哔哩哔哩)wpk辅助破解侠是真的助透视。每...
据监测!wepoker私人局透... 据监测!wepoker私人局透视方法(透视)微乐小程序游戏破解器下载(好像是真的辅助app)-哔哩哔...