Firebase Firestore文档更改 [英] Firebase firestore document changes

查看:71
本文介绍了Firebase Firestore文档更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just want to ask how can I properly use the document changes in my app? Btw there are 3 types of that which is ADDED, MODIFIED and lastly REMOVED. TYPE.ADDED works perfectly fine, but in modified and removed it doesn't work well in modified it. I am using a recyclerview for that and here's my code. Am I wrong utilizing it? Also, I am using a instance oldindex and newindex to know the index which is affected by the action performed.

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)

below code worked for me in 3 conditions ADDED,MODIFIED,REMOVED (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文档更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆