How to tell which descendants are changed with on(“child_changed”)

后端 未结 2 683
半阙折子戏
半阙折子戏 2020-12-15 11:33

For example, I have following database structure:

/ 
+ users
  + 1
    + items
      + -xxx: \"hello\"
  + 2
    + items

Then;



        
相关标签:
2条回答
  • 2020-12-15 11:46

    The child_changed event only provides information on which immediate child has changed. If a node deeper in a data structure changed, you'll know which immediate child was affected but not the full path to the changed data. This is by design.

    If you want granular updates about exactly what changed, you should attach callbacks recursively to all of the elements you care about. That way when an item changes, you'll know what the item was by which callback is triggered. Firebase is actually optimized for this use case; attaching large numbers of callbacks -- even thousands -- should work fine. Behind the scenes, Firebase aggregates all of callbacks together and only synchronizes the minimum set of data needed.

    So, for your example, if you want to get alerted every time a new item is added for any user, you could do the following:

    var usersRef = new Firebase("https://mydb.firebaseio.com/users");
    usersRef.on("child_added", function(userSnapshot) {
      userSnapshot.ref().child("items").on("child_added", function(itemSnapshot) 
        utils.debug(itemSnapshot.val());
      });
    });
    

    If you are working with a very large number of users (hundreds of thousands or millions), and synchronizing all of the data is impractical, there's another approach. Rather than have your server listen to all of the data directly, you could have it listen to a queue of changes. Then when clients add items to their item lists, they could also add an item to this queue so that the server becomes aware of it.

    This is what the client code might look like:

    var itemRef = new Firebase("https://mydb.firebaseio.com/users/MYID/items");
    var serverEventQueue = new Firebase("https://mydb.firebaseio.com/serverEvents");
    itemRef.push(newItem);
    serverEventQueue.push(newItem);
    

    You could then have the server listen for child_added on that queue and handle the events when they come in.

    0 讨论(0)
  • 2020-12-15 11:51

    Andrew Lee gave a nice answer, but I think you should try to use cloud functions. something like this should work:

    exports.getPath = functions.database.ref('/users/{id}/items/{itemId}')
    .onWrite(event => {
      // Grab the current value of what was written to the Realtime Database.
      const original = event.data.val();
      console.log('user id', event.params.id);
      console.log('item id', event.params.itemId);
    });
    
    0 讨论(0)
提交回复
热议问题