1

I'm trying to delete some user from realtime firebase. It's work, but if I put the app in background when I open again the app the user is not deleted from db. In debug mode the user is deleted, but in rest not. I'm using also email/password authentication provide from Firebase.

Here is my code (the methode is call in a onOptionsItemSelected for a menu):

    public void reAuthenticateUser(String password) {
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    AuthCredential credential = EmailAuthProvider
            .getCredential(user.getEmail(), password);
    user.reauthenticate(credential)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(!task.isSuccessful()){
                        handleExceptions(task.getException());
                    }
                    else{
                        dialog.dismiss();
                        Toast.makeText(BaseActivity.this,DBHelper.getInstance().getDatabase().toString(),Toast.LENGTH_LONG).show();
                        DBHelper.getInstance().deleteUser(user.getUid());
                        //ref.child("users").child(user.getUid()).setValue(null);
                        Log.d("ReAuth", "User re-authenticated.");
                        deleteUserAccount();
                    }
                }
            });

}

Here is my DBHelper class:

public class DBHelper {

private static DBHelper instance = null;
private FirebaseDatabase database;
private DatabaseReference ref;

private DBHelper(){
    database = FirebaseDatabase.getInstance();
}

public static DBHelper getInstance() {
    if(instance == null)
        instance = new DBHelper();

    return instance;
}

public void saveUserOnDB(String userId, String name, String email) {
    ref = database.getReference().child("users");
    ref.child(userId).setValue(new User(name, email));
}

public void deleteUser(String userId) {
    ref = database.getReference().child("users");
    ref.child(userId).setValue(null);
}

}

EDIT DB rules:

     {
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}
Gabriela
  • 13
  • 3

1 Answers1

0

private static DBHelper dbHelper = new DBHelper(); may be your problem. When the App is in background, the OS may kill it to free some memory. If you bring it to the front again the activities can be restarted because they store their state to disk, but your static variables are lost.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • I tried to put everything into activity, without using DBHelper class. I instanted ref in onCreate method and I put what is in the deleteUser method in my activity. It's the same problem – Gabriela May 12 '17 at 20:17
  • Seems like you did not implement instance state saving. See http://stackoverflow.com/questions/16769654/how-to-use-onsaveinstancestate-and-onrestoreinstancestate – Henry May 13 '17 at 09:33
  • It's "Firebase Database error: Permission denied". But why if I reauthenticate the user before delete data from db ? – Gabriela May 13 '17 at 12:54