Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google firebase check if child exists

In my app, I need to check if a given element of my database on firebase has a child with a given name. I hoped it could be done by using something along the lines of:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();  if (rootRef.childExists("name")) {     //run some code } 

I searched but I couldn't find anything useful.

like image 850
zomnombom Avatar asked May 23 '16 17:05

zomnombom


People also ask

What is addListenerForSingleValueEvent?

public void addListenerForSingleValueEvent (ValueEventListener listener) Add a listener for a single change in the data at this location. This listener will be triggered once with the value of the data at the location.

Which method of data snapshot checks and returns true if the specified child path has data?

DataSnapshot.hasChild() Returns true if the specified child path has (non-null) data.

Why use Firebase?

Google Firebase offers many features that pitch it as the go-to backend development tool for web and mobile apps. It reduces development workload and time. And it's a perfect prototyping tool. Firebase is simple, lightweight, friendly, and industrially recognized.


2 Answers

Edit 2; worth putting on top: I think it is worth mentioning that this is actually downloading all data at this snapshot just to check whether any data exists. You should be mindful here. If the reference is huge (e.g. actually the root reference and not a specific child/property) then you should either find a deeper node you can use to check for existence or design your data structure differently so an efficient check is possible.

A database reference is effectively the URL for that data. You want to actually get data to see whether a child exists. This is why the method you seem to be looking for is on DataSnapshot.

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); rootRef.addListenerForSingleValueEvent(new ValueEventListener() {   @Override   void onDataChange(DataSnapshot snapshot) {     if (snapshot.hasChild("name")) {       // run some code     }   } }); 

Now, this design pattern feels a bit strange. You're reading the whole database just to see whether "name" exists. You can make this a bit more efficient by listening to rootRef.child("name") and then just checking whether snapshot.exists().

If you're trying to do validation here, and not control flow, you should consider putting this code in your rules.json.

edit: I originally used the wrong function name (childExists instead of hasChild)

like image 64
Thomas Bouldin Avatar answered Sep 22 '22 05:09

Thomas Bouldin


Don't do like this

NEVER

It will take all your data and bring to device

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); rootRef.addListenerForSingleValueEvent(new ValueEventListener() {   @Override   void onDataChange(DataSnapshot snapshot) {     if (snapshot.hasChild("name")) {       // run some code     }   } }); 

Check it by this way. It will return the value of the child if exists, otherwise -> null

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); rootRef.child("childName") rootRef.addListenerForSingleValueEvent(new ValueEventListener() {   @Override   void onDataChange(DataSnapshot snapshot) {     if (snapshot.getValue() == null) {       // The child doesn't exist     }   } }); 
like image 28
Arsen Nersisyan Avatar answered Sep 23 '22 05:09

Arsen Nersisyan