tccpg288
tccpg288

Reputation: 3352

Cannot Remove Child Node in Firebase

I am simply trying to remove a child node in Firebase based on the toggling of a CheckBox. When the CheckBox is clicked, I add the node to Firebase, and when it is unclicked, I want to remove the node. I have read about setting the value to null, however I want to delete it altogether.

mFollowingCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b){
                mBaseRef.child(USERS_LABEL).child(mUserID).child(FOLLOWING_LABEL).child(USERS_LABEL).child(mPollCreatorID).child(USER_ID_LABEL).setValue(mPollCreatorDisplayName);


                });

            } else {
                mBaseRef.child(USERS_LABEL).child(mUserID).child(FOLLOWING_LABEL).child(USERS_LABEL).child(mPollCreatorID).removeValue();


        }
    });

The problem I am running into is when I uncheck the CheckBox, the whole "Users" node is deleted, whereas I simply want the child to be deleted.

Below is the data structure, I am trying to delete/remove 16Yrfh.......

enter image description here

Upvotes: 0

Views: 387

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317828

If you delete the only value in a tree of data, the entire tree will effectively cease to exist. In Firebase Realtime Database, there is no such thing as an "empty node". There is either data present at a node, or the node appears to be gone.

If you had any other data under Users, that node will remain visible in the database.

Upvotes: 3

Related Questions