mercredi 7 février 2018

Firebase: fetch existing data once, then use child_added to get new data

I'd like to just fetch a list once and then have it in memory and use child_added to process items being created.

It's inconvenient for me to only use child_added because doing so would cause me to re-render my entire list for every child_added event. I'd rather render the whole list once at page load, then for each item that's added.

My code:

ref.once('value', (snapshot) => {
    let messages = []
    snapshot.forEach(function(childSnapshot) {
      let messageKey = childSnapshot.key
      let message = childSnapshot.val()
      messages.push(message)
    })
    // Render messages list once

    ref.on('child_added', (message) => {
      const messageVal = message.val()
      // Append message and re-render list
    }
})

The problem is that I still get child_added events for each existing node.

Is the most advisable thing to do just to track which items I've already processed and ignore them in child_added ? Or is there a way to only get new updates?

Thanks




Aucun commentaire:

Enregistrer un commentaire