Pages

How to retrieve specific node from firebase database in android based on value

 Sometimes it is needed to retrieve the node based on a value, not the child.

Android+Firebase


you may get some solution like - to get all the nodes first and after that iterate it locally.

Explaining below - 

Let's assume the requirement is to retrieve all the node that has been mobile number = 9999999999.

Structure of the node - 

Firebase Node

The first approach (Not Recommended) - 


In this approach, you will get all the nodes and then you have to iterate to get the required record.

inline number 15-17 i'm iterating all the nodes and removing  the nodes from the list which has the different mobile number

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private void readAllRequestedFormFromFirebaseDb(){
        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference ref = database.getReference("App_DB");
        DatabaseReference childRef = ref.child("AllRequest");
        childRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                // getting list 
                for(DataSnapshot dataSnapshot: snapshot.getChildren()){
                    QueryFormModel post = dataSnapshot.getValue(QueryFormModel.class);
                    queryFormModelArrayList.add(post);

                }
                for(int i=0;i<queryFormModelArrayList.size();i++){
                    if(!queryFormModelArrayList.get(i).getMobileNumber().equalsIgnoreCase("9999999999"))
                        queryFormModelArrayList.remove(i);
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.e("DetailsActivity", "onCancelled:readAllSubmittedRequestFromFirebaseDb:  "+databaseError );
            }
        });
    }
   

Why this approach is not recommended -?

Just assume if you have 1000 records so in this case, you are getting all the nodes first and after that iterating all the nodes to get what exactly the requirement is.
so in short you are doing irrelevant effort and unnecessary extending the network call.
even though it's correct but it will put a question mark on your programming skills.

The second approach (Recommended) - 

In this approach instead of getting all the nodes, you will get only the nodes that have been the mobile number that you passed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private void readAllRequestedFormFromFirebaseDb(){
        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference ref = database.getReference("App_DB");
        DatabaseReference childRef = ref.child("AllRequest");
        Query queryRef = childRef.orderByChild("mobileNumber").equalTo("9999999999");
        queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                // getting list
                for(DataSnapshot dataSnapshot: snapshot.getChildren()){
                    QueryFormModel post = dataSnapshot.getValue(QueryFormModel.class);
                    queryFormModelArrayList.add(post);
                    /*the above list will have record only
                     with the provided mobile number*/

                }

            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.e("DetailsActivity", "onCancelled:readAllSubmittedRequestFromFirebaseDb:  "+databaseError );
            }
        });
    }
   


That all about this post.👍👍👍

Happy Coding.😍
**********************************************
*************************************************
****************************************************

No comments:

Post a Comment