jonb
jonb

Reputation: 875

How to know if child is null in firebase?

this is the android code which is relevant:

mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()){
                        mLoginProgress.dismiss();
                        final String current_user_id = mAuth.getCurrentUser().getUid();
                        String deviceToken = FirebaseInstanceId.getInstance().getToken();
                        DatabaseReference itsUser = mUsersDatabaseReference.child(current_user_id);
                        DatabaseReference itsPilot = mPilotsDatabaseReference.child(current_user_id);

I have the current_user_id, and I want to know if it's a User or a Pilot. So I have these 2 urls: https://console.firebase.google.com/u/0/project/...-96d4e/database/airpal-96d4e/data/Users/mbGyPElqNphTritK5iyN8bXadSK2

https://console.firebase.google.com/u/0/project/...-96d4e/database/airpal-96d4e/data/Pilots/mbGyPElqNphTritK5iyN8bXadSK2

This is the status: As you can easily see, my current_user_id is 'User' and not a 'Pilot'. I just want to do it by code.

stat

And when I insert them into the chrome in 1 of them the child is null, and in one of them it has fields.

How can I recognize it ?

Reading the dataSnapshot gives me all of the fields seperately and I prefer clearer solution. Thanks

Edit: Solution is by value event listener and snapShot only

itsUser.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Log.d("dDebug","i'm here");
                            pilotBool = (dataSnapshot.getChildrenCount() == 0);
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

Upvotes: 0

Views: 184

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

You can't know anything that's in your database until you query for it to get a snapshot with the contents at the location of the query. There's no special query to check for existence that doesn't involve a snapshot.

Upvotes: 2

Related Questions