0
votes

I know there are lot of questions asked on Firebase Datasnapshot, but still i'm little unclarified about my doubt. Here is the thing,

I've data structer like this in Firebase Realtime database,

enter image description here

If i have a 100kb file inside the above reference say, val ref = FirebaseDatabase.getInstance().getReference("Groups") If i call ` ref.addValueEventListener(object : ValueEventListener {

        override fun onCancelled(p0: DatabaseError) {
        }

        override fun onDataChange(p0: DataSnapshot) {
           // access getvalue here
        }
    })

`

I get a snapshot of data inside the reference as p0.

Question-1 Will this particular snapshot has all the available data inside the reference (will this downloaded snapshot size is 100kb?). If i cut the internet at the first line on onDataChange, can i be able to access all the data inside datasnapshot Or Will the data be downloaded only when i call p0.getValue command on onDataChange & datasnapshot is just like data reference?

Question-2

In my code i've put a ValueEventListener on "Groups", so if there is a single change in the person2 activity(either new activity is added or existing data is modified), I'm downloading the entire data in the "Groups", which i wanted to avoid as the groups become larger more data is consumed. How to effectively get data from only person2 when person2 data modified or new activity added. I've tried adding child event listener to "Groups", which behaves same as ValueEventListener. I can't add listener for each person as the number of persons are unknown and it can be a larger number, i dont wan't that many listener in my code. My code is in production already so i can't even change the database structure.

1

1 Answers

1
votes
  1. Yes, the snapshot will contain everything in memory at the moment you receive it in your callback. You don't even have to reach into the snapshot at all.

  2. With ValueEventListener, if anything under the snapshot changes while the listener is attached, only the changes will be transferred. The entire snapshot is not downloaded entirely for each change to a child. However, you will have to write code to determine exactly what changed in the snapshot. This could be difficult, as you will have to compare all the child values to each other from the prior and new snapshot.