删除一篇文章后,Firestore快照无法检索某些数据

时间:2018-12-28 14:06:29

标签: android firebase google-cloud-firestore

我正在开发像博客一样的应用程序。问题是,当我删除帖子时,即使它们仍在数据库控制台中,我也无法获取旧帖子,即迷路了。但是在清除了数据和应用程序存储的缓存后,我得到了所有帖子。问题似乎与离线存储的数据有关。

此问题并非总是发生,有时会发生。插入以下代码后,它可以恢复工作:

firebaseFirestore= FirebaseFirestore.getInstance();
    FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
            .setPersistenceEnabled(false)
            .build();

    firebaseFirestore.setFirestoreSettings(settings);`

但是我需要它也可以在脱机模式下工作。

请有人帮我。

这是我删除帖子的方式:

holder.blogDelete.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("Do you want to delete this Post!")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        firebaseFirestore.collection(departmentof).document(blogPostId).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                               blog_list.remove(holder.getAdapterPosition());
                               user_list.remove(holder.getAdapterPosition());
                               notifyDataSetChanged();
                            }
                        });
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                }).show();
        return true;
    }
});

这是我检索帖子的方式:

     blog_list_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Boolean reachedBottom= !recyclerView.canScrollVertically(1);
                if(reachedBottom){
                    loadMorePosts();
                }
            }
        });
        Query firstQuery= firebaseFirestore.collection(departmentName).orderBy("timestamp", Query.Direction.DESCENDING).limit(3);
            firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                    if(documentSnapshots!=null){
                        BlogRecyclerAdapter.DrAbiyAhmed=!documentSnapshots.getMetadata().hasPendingWrites();
                        if(!documentSnapshots.isEmpty()){
                            if(isFirstPageFirstLoad){
                                lastVisible= documentSnapshots.getDocuments().get(documentSnapshots.size()-1);
                                blog_list.clear();
                                user_list.clear();
                            }

                            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                                if (doc.getType() == DocumentChange.Type.ADDED) {
                                    String bloPostId= doc.getDocument().getId();
                                    final BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(bloPostId,departmentName);
                                    //Log.d("aman","size="+",brakinge="+braeaking);

                                    String blogUserId= doc.getDocument().getString("user_id");
                                    firebaseFirestore.collection("Users").document(blogUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                            if(task.isSuccessful()){
                                                User user = task.getResult().toObject(User.class);
                                                if(isFirstPageFirstLoad){
                                                    user_list.add(user);
                                                    blog_list.add(blogPost);
                                                    // Toast.makeText(getActivity(),""+blog_list.size(),Toast.LENGTH_SHORT).show();
                                                }
                                                else{
                                                    user_list.add(0,user);
                                                    blog_list.add(0,blogPost);
                                                    //Toast.makeText(getActivity(),""+blog_list.size(),Toast.LENGTH_SHORT).show();

                                                }

                                                // i++;
                                                //if(documentSnapshots.getDocumentChanges().size()==i){
                                                isFirstPageFirstLoad=false;
                                                //}
                                                blogRecyclerAdapter.notifyDataSetChanged();
                                                mProgressBar.setVisibility(View.INVISIBLE);
                                                braeaking=false;
                                                // Log.d("aman","braking"+braeaking);
                                            }else  {
                                                // Log.d("aman","size=c"+j);
                                                String errorMessage= task.getException().getMessage();
                                             //   if(DepartmentsFragment.this.isVisible()&&isAdded())
                                              //  Toast.makeText(getActivity(),"Error "+errorMessage,Toast.LENGTH_LONG).show();
    }
});

compile 'com.android.support:multidex:1.0.3'
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support:design:27.1.1'
compile 'com.balysv.materialmenu:material-menu:2.0.0'
compile 'xyz.danoz:recyclerviewfastscroller:0.1.3'
compile 'com.google.firebase:firebase-firestore:15.0.0'
compile 'com.google.firebase:firebase-storage:15.0.0'
compile 'com.google.firebase:firebase-core:15.0.0'
compile 'com.google.firebase:firebase-auth:15.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'org.jsoup:jsoup:1.10.1'
compile 'id.zelory:compressor:2.1.0'
compile 'com.android.support:recyclerview-v7:27.0.2'
compile 'com.android.support:support-v4:27.1.1'
compile 'com.android.support:cardview-v7:27.0.2'
compile 'com.theartofdev.edmodo:android-image-cropper:2.5.1'
compile 'de.hdodenhof:circleimageview:2.2.0'
compile 'com.github.bumptech.glide:glide:4.8.0'

testCompile 'junit:junit:4.12'
androidTestCompile 'com.google.code.findbugs:jsr305:3.0.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

请有人帮我...

0 个答案:

没有答案