2

I've following tree structure in firebase. enter image description here

Now in AndroidStudio I want to fetch data from children of "en-US" (which in the current case are the nodes 1552040445614 and 1552040702223).

When I only want values of keys "id" and "text2", so I've written a data model class in Java like this:

public class MyDataClass {
 public String id;
 public String text2;

 public MyDataClass() {

 }
}

And I'm fetching data like this:

    ArrayList<MyDataClass> listDataClass = new ArrayList<>();
    mDatabase = FirebaseDatabase.getInstance().getReference().child("en-US");
    mDatabase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            MyDataClass myDataClass = datasnapshot.getValue(MyDataClass.class);
            listDataClass.add(myDataClass);

        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            // getContents(dataSnapshot);
        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

Now the question is: Although I am only fetching values from keys "id" and "text2" from each child, there are other fields (like meta, listOfTexts1 etc.) also. Will these be also fetched in this query and consume from 10GB/month limit of free firebase account? If yes, then how can I optimize for only getting "id" and "text2" values?

Thanks in advance.

KENdi
  • 7,576
  • 2
  • 16
  • 31
Faraz Ahmad
  • 515
  • 1
  • 6
  • 13
  • There is no way to read only certain fields from nodes with Firebase. See https://stackoverflow.com/questions/51079674/read-only-certain-fields-in-firebase – Frank van Puffelen Mar 08 '19 at 15:39
  • I think this article, [How to optimize Firebase Realtime Database calls to improve performance?](https://medium.com/firebase-tips-tricks/how-to-optimize-firebase-realtime-database-calls-to-improve-performance-cc63dad374d5) might help. – Alex Mamo Dec 14 '21 at 07:25

1 Answers1

2

Will these be also fetched in this query and consume from 10GB/month limit of free firebase account?

Yes, it will. When you perform a query at a specific level, the dataSnapshot object that you get contains all the properties and all the object beneath it. So assuming you are adding a listener on the root node, you'll be downloading the entire database. This is incredibly wasteful of bandwidth, especially as your database grows.

If yes, then how can I optimize for only getting "id" and "text2" values?

If you need only the id and text2 fieds, it is recommended to create another node that will contain objects with only those two properties (without meta, listOfTexts1 etc). This practice is called denormalization and is a common practice when it comes to Firebase. If you are new to NoQSL databases, I recommend you see this video, Denormalization is normal with the Firebase Database for a better understanding, .

Also, when you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for nice explanation. – Faraz Ahmad Mar 08 '19 at 11:47
  • Now I have two nodes node1 contains children with basic info (id and text2), while node2 contains (meta and nested list data, and an extra field to store reference to the same child in node1). I fetch all children from node1 in a listview in android. When I click on a particular list item (child of node1), it should get me the details (from node2) of that particular child only. So I am querying node2 and search with .orderBy for the reference from node1. Doesn't it fetch full snapshot from node2, and the overall bandwidth consumption stays the same? – Faraz Ahmad Mar 08 '19 at 11:54
  • 1
    You're welcome! So when you are getting the data from node1 that only contains basic info (id and text2) you'll be charged only for those properties. If you click to get the data from node2, you be charged with the entire object (but only if you click). Is it clear now? – Alex Mamo Mar 08 '19 at 11:59
  • Yeah, thanks again. Final question, does using .orderBy also helps reduce bandwith consumption (apart from just filtering data for me)? – Faraz Ahmad Mar 08 '19 at 12:00
  • No, calling `.orderBy` will not help you saving bandwith consumption. It's just ordering your elements. – Alex Mamo Mar 08 '19 at 12:07
  • Sorry, I meant to ask .orderBy combined with .equalTo() can reduce bandwidth consumption? – Faraz Ahmad Mar 08 '19 at 12:18
  • Moreover, I was working on new solution (having two nodes node1 and node2). I fetch node1 in listview in android, then click on a list item that will fetch all data from node2 and open a new activity. Now I come back to previous activity (containing listview from node1) and click an item again that will again fetch node2 all data, so on. So isn't it consuming more bandwith in case the users move back n forth across activities? – Faraz Ahmad Mar 08 '19 at 12:30
  • 1
    No, if you enable offline persistence using `setPersistenceEnabled(true);`. This means that you get the data once and second you get it from the cache. – Alex Mamo Mar 08 '19 at 12:38