I am working on app that works like a blog. The problem is when I delete a post I couldn't get the old post , the get lost, even if they are still there in the database console. But after I clear the data and the cache stored by the app I get all posts back. The problem seems with the data stored offline. This problem didn't occurs always, It occurs sometimes. after inserting the following code it works back.`

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

    firebaseFirestore.setFirestoreSettings(settings);`

But I need it work also in Offline mode. Somebody help me please. This How I delete posts...

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

This How I retrieve posts....

     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'

Somebody help me please...

0 Answers