首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于Fi还原文档的子事件侦听器和多个用户同时写入文档?

用于Fi还原文档的子事件侦听器和多个用户同时写入文档?
EN

Stack Overflow用户
提问于 2017-10-11 13:50:45
回答 2查看 5.7K关注 0票数 0

是否有任何方法为Fi还原文档实现ChildEventListener (即当字段被添加/移除到文档中时,必须触发只获取添加/删除字段快照的侦听器)。最好,它必须具有OnChildAdded、OnChildChanged和OnChildDeleted功能,类似于Firebase实时数据库。

另外,是否有任何方法可以让多个用户一次不发生冲突地操作单个Firestore文档?(例如,用户1、2和3同时将其名称添加到同一文档中)。您能在Android中提供这方面的实现吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-11 15:25:22

在Cloud中,文档的字段不像处理实时数据库路径时那样被视为文档的“子”,因此没有办法完全按照您的要求执行。

不过,您可以监听文档上的实时更改,并实现自己的差异,找出哪些字段更改了:

代码语言:javascript
运行
复制
final DocumentReference docRef = db.collection("cities").document("SF");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(@Nullable DocumentSnapshot snapshot,
                        @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "Listen failed.", e);
            return;
        }

        // ... calculate which fields changed ...
    }

});
票数 1
EN

Stack Overflow用户

发布于 2018-06-16 09:54:02

这是如何在FireStore中这样做的,您需要使用添加的、修改的、删除的枚举。

代码语言:javascript
运行
复制
 //Listener
        update_listener = mDatabase.collection("Announcements").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                //If something went wrong
                if (e != null)
                    Log.w(TAG, "ERROR : ", e);

                if (queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()) {
                    //Instead of simply using the entire query snapshot
                    //See the actual changes to query results between query snapshots (added, removed, and modified)
                    for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                        switch (doc.getType()) {

                            case ADDED:

                                if (!isFirstListLoaded){
                                    //Call the model to populate it with document
                                    AnnouncementModel annonPost = doc.getDocument().toObject(AnnouncementModel.class)
                                            .withId(doc.getDocument().getId());
                                    //This will be called only if user added some new post
                                    announcementList.add(0, annonPost);
                                    announcementRecyclerAdapter.notifyItemInserted(0);
                                    //Notify the adapter to update all position
                                    announcementRecyclerAdapter.notifyItemRangeChanged(0, announcementList.size());

                                    Log.d(TAG,"THIS SHOULD BE CALLED");

                                   /* //Just call this method once
                                    if (noContent.isShown()){
                                        //This will be called only if user added some new post
                                        announcementList.add(annonPost);
                                        announcementRecyclerAdapter.notifyDataSetChanged();
                                        noContent.setVisibility(View.GONE);
                                        label.setVisibility(View.VISIBLE);
                                    }*/
                                }

                                break;

                            case MODIFIED:
                                break;

                            case REMOVED:
                                //Get the document ID of post in FireStore
                                //Perform a loop and scan the list of announcement to target the correct index
                                for (int i = 0; i < announcementList.size(); i++) {
                                    //Check if the deleted document ID is equal or exist in the list of announcement
                                    if (doc.getDocument().getId().equals(announcementList.get(i).AnnouncementsID)) {
                                        //If yes then delete that object in list by targeting its index
                                        Log.d(TAG, "Removed Post: " + announcementList.get(i).getTitle());
                                        announcementList.remove(i);
                                        //Notify the adapter that some item gets remove
                                        announcementRecyclerAdapter.notifyItemRemoved(i);
                                        //Notify the adapter to update all position
                                        announcementRecyclerAdapter.notifyItemRangeChanged(i, announcementList.size());
                                        break;
                                    }
                                }
                                break;
                        }
                    }

                    isFirstListLoaded = false;
                }

            }
        });
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46689970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档