Gowthaman M
Gowthaman M

Reputation: 8282

Ui strucking Firebase Firestore deleting datas?

I am using Firebase Firestore I want to remove data from database..removing data woking fine but my progress dialog get stucking.I think i have to use Worker thread,but i donot know how to use.

 db = FirebaseFirestore.getInstance();
            db.collection("expenses").document(ids)
                    .delete()
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {

                             Toast.makeText(Expense_Summary.this, "successfully deleted file", Toast.LENGTH_SHORT).show();
                            result = "deleted";
                            hideProgressDialog();
                            finish();

                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.w("deleting f", "Error deleting document", e);
                            hideProgressDialog();
                            Toast.makeText(Expense_Summary.this, "Error deleting file", Toast.LENGTH_SHORT).show();
                        }
                    });

I Refered doc : https://firebase.google.com/docs/firestore/manage-data/delete-data

Upvotes: 0

Views: 122

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80924

Try this:

 final ProgressDialog progressDialog = ProgressDialog.show(Activity_name_here.this,"Please wait","Processing",true);
                            Runnable runnable=new Runnable() {
                                @Override
                                public void run() {
                                    progressDialog.cancel();
                                }
                            };
                            Handler pdCanceller = new Handler();
                            pdCanceller.postDelayed(runnable, 3000);
 db = FirebaseFirestore.getInstance();
        db.collection("expenses").document(ids)
                .delete()
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {

                         Toast.makeText(Expense_Summary.this, "successfully deleted file", Toast.LENGTH_SHORT).show();
                        result = "deleted";
                       progressDialog.dismiss();
                        finish();

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.w("deleting f", "Error deleting document", e);
                        progressDialog.dismiss();
                        Toast.makeText(Expense_Summary.this, "Error deleting file", Toast.LENGTH_SHORT).show();
                    }
                });

Using the code above a progress Dialog will be shown with text Please wait and a small circle next to it written is processing.

This: pdCanceller.postDelayed(runnable, 3000); specifies the time example here I'm using 3000 milliseconds. Then if it is successful the progress dialog will be dismissed. Or if it fails also the progress dialog will be dismissed.

Upvotes: 1

Related Questions