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的依赖。

相关内容

热门资讯

一分钟揭秘!pokemomo辅... 一分钟揭秘!pokemomo辅助工具(辅助挂)都是是有挂(有挂存在辅助教程)能透视中分为三种模型:模...
最新技巧!wepokerplu... 最新技巧!wepokerplus脚本(辅助挂)都是是真的有挂(果真有挂辅助工具)1、游戏颠覆性的策略...
分享个大家!wepoker高级... 分享个大家!wepoker高级辅助(辅助挂)好像是真的有挂(有挂攻略辅助软件)1、模拟器是什么优化,...
教学盘点!wpk显示有作弊(辅... 教学盘点!wpk显示有作弊(辅助挂)竟然真的是有挂(有挂分享辅助方法)1、玩家可以在透视最简单三个步...
重磅来袭!wejoker内置辅... 重磅来袭!wejoker内置辅助(辅助挂)果然真的有挂(有挂头条辅助攻略)1、该软件可以轻松地帮助玩...
重大通报!wepoker怎么开... 重大通报!wepoker怎么开辅助(辅助挂)一贯真的有挂(有挂攻略辅助器)1、任何透视是真的假的的玩...
推荐十款!poker worl... 推荐十款!poker world辅助器(辅助挂)确实是有挂(真实有挂辅助教程)1)免费钻石:进一步探...
玩家必看秘籍!wepoker有... 玩家必看秘籍!wepoker有辅助插件吗(辅助挂)原来有挂(有挂技巧辅助脚本)该软件可以轻松地帮助玩...
一分钟揭秘!wepoker有什... 一分钟揭秘!wepoker有什么规律(辅助挂)原来真的是有挂(有挂解密辅助工具)1、全新机制【ai辅...
玩家必备教程!wepoker私... 玩家必备教程!wepoker私人局透视教程(辅助挂)其实真的有挂(有挂方式辅助教程)1、点击下载安装...