Firestore文档的子事件侦听器和多个用户一次写入文档?

时间:2017-10-11 13:50:46

标签: android google-cloud-firestore

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

此外,有多种方法可以让多个用户同时操作单个Firestore文档而不会发生冲突? (例如,用户1,2和3同时将他们的名字添加到同一文档中)。你能在Android中提供这方面的实现吗?

2 个答案:

答案 0 :(得分:1)

这是如何在FireStore中执行此操作,您需要使用ADDED,MODIFIED,REMOVED枚举。

 //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;
                }

            }
        });

答案 1 :(得分:0)

在Cloud Firestore中,文档的字段不像处理实时数据库路径时那样被视为文档的“子项”,因此无法完全按照您的意愿执行操作。

您可以收听文档的实时更改,并自行实现差异以找出更改的字段:

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 ...
    }

});