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

相关内容

热门资讯

透视积累!wepoker免费钻... 透视积累!wepoker免费钻石(透视)果然真的有辅助神器(哔哩哔哩)1、任何wepoker免费钻石...
透视方式!wepoker祈福有... 透视方式!wepoker祈福有用吗(透视)真是存在有辅助软件(哔哩哔哩)1、上手简单,内置详细流程视...
透视绝活儿!wepoker底牌... 透视绝活儿!wepoker底牌透视(透视)一直有辅助软件(哔哩哔哩)1、玩家可以在wepoker底牌...
透视绝活儿!德州圈脚本(透视)... 透视绝活儿!德州圈脚本(透视)原来存在有辅助软件(哔哩哔哩)1、任何德州圈脚本透视是真的假的的玩家都...
透视讲义!wepoker怎么看... 透视讲义!wepoker怎么看底牌(透视)其实存在有辅助安装(哔哩哔哩)进入游戏-大厅左侧-新手福利...
专业透视!安装不了wepoke... 专业透视!安装不了wepoker,wepoker辅助分析器,都是是有辅助方法(哔哩哔哩)一、可以开透...
必备透视!安装不了wepoke... 必备透视!安装不了wepoker,wepoker有没有辅助,切实有辅助技巧(哔哩哔哩)1、打开软件启...
辅助透视!aapoker如何设... 辅助透视!aapoker如何设置胜率,aapoker怎么设置提高好牌几率,原来真的是有辅助攻略(哔哩...
有挂透视!wpk辅助器是真的吗... 有挂透视!wpk辅助器是真的吗,wpk可以作比弊吗,本来是有辅助教程(哔哩哔哩)1、打开软件启动之后...
透视透视!德普之星辅助工具如何... 透视透视!德普之星辅助工具如何设置,德普之星私人局辅助器,原来一直都是有辅助技巧(哔哩哔哩)1、每一...