Why does the Firebase event 'child_added' fire after the 'value' event?

后端 未结 1 467
名媛妹妹
名媛妹妹 2021-01-23 08:53

According to the Firebase documentation:

Value events are always triggered last and are guaranteed to contain updates from any other even

相关标签:
1条回答
  • 2021-01-23 09:51

    To summarize the excellent feedback in the comments and by Firebase Support:

    1. It makes most sense here to use on() instead of once() to register the event listener. In the example of the original post, and to quote Firebase Support:

      The on() callback is registered, and when the once() callback is registered it is ordered in reference to the on() callback. Once the once() callback is fired, it's automatically deregistered. Even though the execution of the events are done in a certain order (due to javascript being single threaded), they are being calculated separately from each other.

      Frank's correction to that example shows this in action.

    2. The modified example again breaks the "guarantee" because (Firebase Support):

      The data is already locally on the client. So once you have run ref.child('pets').on() and the callback happens, all the data under /pets has been retrieved to the client. Now in the callback processing, you are adding additional callbacks to the existing data. When the callback is being added, the client library is immediately firing the callback without waiting for the second one to be registered since all the data is available.

      Since I would like to enforce the guarantee in this case where the data is local, I simply register the child_added listener before the value listener, as demonstrated in the correction to the modified example.

    0 讨论(0)
提交回复
热议问题