Provest Teo
Provest Teo

Reputation: 13

How to remove the child node while retaining the parent firebase

Firebase Tree Structure

From the image above, I want to remove all the child under item3 without removing parent item3, 1234567 and User View.

Following is what I did. Remove is successful but all the parents node are also removed since there is no data inside.

This results an error of null object reference as cartRef cant be identified in Firebase. The only solution I thought to resolve this error is to retain the parent node. Do guide me if there is anymore solution to it.

cartRef = FirebaseDatabase.getInstance().getReference().child("Cart List").child("User View").child(Prevalent.currentOnlineUser.getPhone()).child("item3");

cartRef.removeValue();

Upvotes: 1

Views: 58

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

If you remove item3, the only child of 1234567, you will always implicitly remove 1234567 because it no longer has children. There is no such thing as an "empty node" in Realtime Database. When a node not longer has children, it is effectively gone, and when you query it, it will yield no data.

Upvotes: 2

Related Questions