Pushpendra
Pushpendra

Reputation: 1519

How to find the sub child in Firebase in Java Android

I have seen a couple of answers, But I didn't get help from those! Might be I am wrong. But here I am trying to find the item where key = user_id inside split_management. Below is the image!

enter image description here

And below is the code which is not working as expected!

public synchronized void fetchGroups(final IConstants.CallBack callBack) {
    Models.User model = AppController.instance.getUser();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference mDatabaseReference = database.getReference();
    Query query = mDatabaseReference.child(IConstants.DBReference.split_management).orderByChild(model.getUid());
    query.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String previousChildKey) {
            if (dataSnapshot.exists()) {
                if (callBack != null) {
                    Models.Group group = dataSnapshot.getValue(Models.Group.class);
                    callBack.callBack(group);
                }
            }
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, String s) {
            if (callBack != null) {
                callBack.callBack(IConstants.Key.updated);
            }
        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
            if (callBack != null) {
                callBack.callBack(IConstants.Key.updated);
            }
        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, String s) {
            if (callBack != null) {
                callBack.callBack(IConstants.Key.updated);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            if (callBack != null) {
                callBack.callBack(null);
            }
        }
    });

I am not getting proper output! I will very thank full to you all if you could provide me a solution for this! I want resultant query like - Fetch record where key(Which is inside random) = 'SOME_KEY'(Which is User ID)

Upvotes: 0

Views: 51

Answers (1)

samthecodingman
samthecodingman

Reputation: 26171

Filter out null values returned in your query results by specifying a start value of false:

 Query query = mDatabaseReference.child(IConstants.DBReference.split_management).orderByChild(model.getUid()).startAt(false);

See How query data is ordered for why false was used here.

However, this query is ill-advised as it requires the client to perform the sort as the data is not indexable. I would advise keeping a list of all split_management IDs linked to the desired user in /users/someUserId/split_management_ids or similar, like so:

{
  "/users/someUserId/split_management_ids": {
    "251219141300803": true,
    "564634584234535": true
  }
}

Upvotes: 3

Related Questions