Swati Sharma
Swati Sharma

Reputation: 1

Firebase-Database inconsistence data being stored on database in Android

I'm new to firebase. i'm trying to store some data in Firebase-database in the given format:

approot{
uid{
name:"name"
add:"add"
phn:"phn"
}
uid2{
name:"name"
add:"add"
phn:"phn"
}
.......

and so on. for this i have written the following code:

String phn = inputPhn.getText().toString().trim();
        String add = inputAdd.getText().toString().trim();
        String name = inputName.getText().toString().trim();

        if (TextUtils.isEmpty(name)) {
            Toast.makeText(getApplicationContext(), "Enter name!", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(phn)||phn.length()!=10) {
            Toast.makeText(getApplicationContext(), "Enter phone no.!", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(add)) {
            Toast.makeText(getApplicationContext(), "Enter address!", Toast.LENGTH_SHORT).show();
            return;
        }
        //String info="Name:"+name+"\n"+"PhoneNo:"+phn+"\n"+"Adress:"+add+"\n";
        String uId=auth.getCurrentUser().getUid();
        myref.child(uId).child("name").setValue(name);
        myref.child(uId).child("add").setValue(add);
        myref.child(uId).child("phone").setValue(phn);
        myref.child(uId).child("end").setValue("end");
        auth.signOut();

my problem is that i'm getting in-consistent data storage in firebase database. the data stored in database is this :

{
  "User" : {
    "ANj8GLEJS9alW11QYQyF8Rykwyn2" : {
      "add" : "hzjs",
      "name" : "hxjx"
    },
    "b0ZBftQ5UOOtTtzkeZHcC0558MC3" : {
      "add" : "gw",
      "name" : "hs"
    },
    "eH6rCXzvEncTe163N7KaJ7XEZp52" : {
      "name" : "dudj"
    },
    "gEqc92Z0mvcfIWvhZu7QOhRcnNQ2" : {
      "add" : "bsus",
      "name" : "jziz",
      "phone" : "1234567890"
    },
    "kGF2PalHrpeFus6682z55p47YBg2" : {
      "add" : "hdud",
      "name" : "jdhd"
    },
    "nHRt5n5roGfU3QogVUM09HDBrIw2" : {
      "add" : "qwerty",
      "end" : "end",
      "name" : "q",
      "phone" : "1234567860"
    },
    "pRRzexjQN1eynoCQcoClNl2TcHk2" : {
      "add" : "dvjh",
      "name" : "rhid"
    },
    "vaek3Z4eEVTWmc722T92tfgBwaE3" : {
      "add" : "dhudvj",
      "name" : "gsbud"
    },
    "xsH5JoTXftaxicUmvntt0N5hcNJ3" : {
      "add" : "zhs",
      "name" : "q"
    }
  }
}

please help me.
Edit
i'm using firebase 9.2.1. and the rules defined are:-

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

Upvotes: 0

Views: 115

Answers (1)

Wilik
Wilik

Reputation: 7720

You should wait for the setValue to finish before you sign out the user, because the auth will be null after the signOut successfully called. If the setValue haven't finished, it will violate your rule because the auth has been null.

You can use a CompletionListener to make sure that the setValue is completed, then you can safely sign out the user. Like this

Map<String, String> values = new HashMap<>();
values.put("name", name);
values.put("add", add);
values.put("phone", phn);
values.put("end", end);

DatabaseReference.CompletionListener completionListener = new DatabaseReference.CompletionListener() {
    @Override
    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
        auth.signOut();
    }
};
myref.child(uid).setValue(values, completionListener);

Upvotes: 2

Related Questions