Issue
The following code spends 1MB to extract data from the firebase. Is this normal? What can I do to reduce it? Because it's way too costly. I think there's a better way. Can someone help?
mDatabaseRef.child("FalMetinleri").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long KapasiteGiris= dataSnapshot.child("giris").child(finalKategoriCinsiyet).child(finalKategoriGiris).getChildrenCount();
long KapasiteKarsilama= dataSnapshot.child("karsilama").getChildrenCount();
long KapasiteAskDurumu1= dataSnapshot.child("askDurumu").child(finalKategoriCinsiyet).child(finalKategoriAskdurumu).getChildrenCount();
long KapasiteAskDurumu2= dataSnapshot.child("askDurumu2").child(finalKategoriCinsiyet).child(finalKategoriAskdurumu).getChildrenCount();
long KapasiteisDurumu1= dataSnapshot.child("isDurumu").child(finalKategoriCinsiyet).child(finalKategoriMeslek).getChildrenCount();
long KapasiteisDurumu2= dataSnapshot.child("isDurumu2").child(finalKategoriCinsiyet).child(finalKategoriMeslek).getChildrenCount();
long KapasiteBitis= dataSnapshot.child("bitis").child(finalKategoriCinsiyet).child(finalKategoriBitis).getChildrenCount();
kapasite[0] = KapasiteGiris;
kapasite[1] = KapasiteKarsilama;
kapasite[2] = KapasiteAskDurumu1;
kapasite[3] = KapasiteAskDurumu2;
kapasite[4] = KapasiteisDurumu1;
kapasite[5] = KapasiteisDurumu2;
kapasite[6] = KapasiteBitis;
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
dialogFragment.dismissAllowingStateLoss();
}
});
Output:
KapasiteGiris:8
KapasiteKarsilama:5
KapasiteAskDurumu1:4
KapasiteAskDurumu2:14
KapasiteisDurumu1:5
KapasiteisDurumu2:4
KapasiteBitis:13
CLI report:
Solution
The only bit of code that matters here is the query that gets a listener attached:
mDatabaseRef.child("FalMetinleri").addListenerForSingleValueEvent()
What this is doing is fetching everything under the node "FalMetinleri". All other code inside the listener doesn't cost any more data, since the entire snapshot of that child will already be in memory. It looks like that snapshot has about 1MB of data in it.
If you don't want the entire contents of that child, then you can individually request each nested child within it. This will require one query for each child, and a new listener for each child.
Answered By - Doug Stevenson
Answer Checked By - Gilberto Lyons (JavaFixing Admin)