Home > Software design >  Firebase Realtime Database stream onchildchanged
Firebase Realtime Database stream onchildchanged

Time:10-17

I am trying to get the changed data only using onchildchanged stream event in flutter, but the problem is it is getting every data under that node which are unchanged.

My Database:

Database photo

Here if I am streaming rooms node, and try to change the room name ,stream is getting all the data under rooms node ,where I only want the changed node data which is room name in this case.

Flutter code :

Query _userdIdQuery = FirebaseDatabase.instance.reference().child("users").child(FirebaseAuthData.auth.currentUser!.uid).child("rooms").orderByKey();
userIdDataAddSubscription = _userdIdQuery.onChildChanged().listen((event){
      print(event.snapshot.value);
    });

I can compare the data with my local data in modal class, but I want a efficient/less data consuming method which is avoid getting unchanged data. Is there any solution to fix it?

Edit: After some research I found that all the data under streaming node will be received when data changes so I think there is no other way to get only changed data. Currently, I am thinking to stream multiple nodes ,will streaming multiple node impact app performance and network usage?

CodePudding user response:

What you're seeing is the expected behavior. When you subscribe to onChanged on /users/$uid/rooms, you will get called whenever a room changes and you will get a snapshot of the entire room.

The server may send less data to the client than the entire node, but the SDK will always call your code with the complete snapshot of the room. If you want to know exactly what has changed in the room, you'll have to compare the previous data with the new snapshot in your application code.

  • Related