Hammad Nasir Hammad Nasir - 2 months ago 13
Android Question

Value not getting removed from firebase

I'm trying to remove some value stored at a particular reference on firebase.

Here's how I have stored it:

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).setValue(MainActivity.uid);


Here's how I'm trying to remove it:

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).child(MainActivity.uid).removeValue();


itemIDFU
has been generated in
onCreate()
using this code:

itemIDFU = mDatabase.child("users").child(uid).push().getKey();


The problem is that it is getting stored successfully, but not getting removed.

What's going wrong here?

Please let me know.

Answer

It looks like when you set the value, you set the key itemIDFU to equal whatever value is in MainActivity.uid

However, when you delete the data, you are trying to get the child of itemIDFU, which doesn't exist.

You didn't add MainActivity.uid as the child of itemIDFU, you added it as the value.

You have two possible solutions:

Try this if you want to remove the value, but leave the itemIDFU key there empty:

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).setValue(null);

Try this if you want to completely remove the key and value:

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).removeValue();

Here is a picture to help illustrate the data and the problem:

enter image description here

One more picture to explain

enter image description here