Firebase firestore 文档更改

标签 firebase google-cloud-firestore

我只想问如何在我的应用程序中正确使用文档更改?顺便说一句,有 3 种类型,即添加、修改和最后删除。 TYPE.ADDED 工作得很好,但是在修改和删除中它在修改它时效果不佳。我正在使用 recyclerview,这是我的代码。我使用它有错吗?另外,我使用实例 oldindex 和 newindex 来了解受所执行操作影响的索引。

for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
    if(doc.getType() == DocumentChange.Type.ADDED) {
        PostsClass post = doc.getDocument().toObject(PostsClass.class).withId(doc.getDocument().getId());
        postList.add(post);
        Log.d(TAG, post.toString());
        postsAdapter.notifyDataSetChanged();
    }
    else if (doc.getType() == DocumentChange.Type.MODIFIED) {
        adoptList.clear();
        AdoptClass adopt = doc.getDocument().toObject(AdoptClass.class).withId(doc.getDocument().getId());
        adoptList.add(adopt);
        adoptListAdapter.notifyItemChanged(oldIndex);
    }
    else  if (doc.getType() == DocumentChange.Type.REMOVED) {
        adoptList.remove(oldIndex);
        adoptListAdapter.notifyItemRemoved(oldIndex);
    }
}

最佳答案

下面的代码在添加、修改、删除的 3 种情况下对我有效(Android Firestore)

 for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
                    if (documentChange.getType() == DocumentChange.Type.ADDED) {

                        String doc_id = documentChange.getDocument().getId();
                        PostModel postModel = documentChange.getDocument().toObject(PostModel.class).withDocId(doc_id);
                        postModelList.add(postModel);

                    } else if (documentChange.getType() == DocumentChange.Type.MODIFIED) {

                         // modifying

                        String docID = documentChange.getDocument().getId();
                        PostModel changedModel = documentChange.getDocument().toObject(PostModel.class).withDocId(docID);
                        if (documentChange.getOldIndex() == documentChange.getNewIndex()) {
                            // Item changed but remained in same position
                            postModelList.set(documentChange.getOldIndex(),changedModel);
                            postListAdapter.notifyItemChanged(documentChange.getOldIndex());
                        }else {
                            // Item changed and changed position
                            postModelList.remove(documentChange.getOldIndex());
                            postModelList.add(documentChange.getNewIndex(),changedModel);
                            postListAdapter.notifyItemMoved(documentChange.getOldIndex(),documentChange.getNewIndex());
                        }

                        postListAdapter.notifyDataSetChanged();
                    } else if (documentChange.getType() == DocumentChange.Type.REMOVED) {

                        // remove
                        postModelList.remove(documentChange.getOldIndex());
                        postListAdapter.notifyItemRemoved(documentChange.getOldIndex());
                    }


                }

关于Firebase firestore 文档更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50754912/

相关文章:

android - FireStorePagingAdapter 未收到实时更新

javascript - Firebase:数据库 + 存储 - 引用帖子正确图像的最佳实践 | Vue.js

firebase - Firestore 安全规则 : check for not exists(/collection/document)

firebase - 在 Android 设备上发布时未发送 OTP

ios - 类似 swift firebase 的按钮

firebase - Google Firestore 是 Google Cloud Datastore 的子集还是超集?

android - 库版本 11.2.0 的 firebase ProGuard 错误(警告)

flutter - flutter 朔迷离的Firestore Query Snaphot是否永远为空?

android - 如何在不触发 OnRatingBarChangeListener 的情况下将 RatingBar 设置为具有固定星星的指标

node.js - 如何在使用函数接收到集合的创建事件时更新/插入云 Firestore 中的其他文档