0

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

1 Answer 1

2

If you introduce a way to determine exactly which items are "new" at any given point in time, you can use that in your 'child_added' query to pick up where a 'value' query left off. For example, if your data items have a timestamp associated with each item, and you know the latest timestamp from all the items you received from a 'value' query, modify the 'child_added' query to filter for only items >= that timestamp.

If your data doesn't help you create a natural time-based ordering of your items like this, or you've already exhausted your one allowed filter for the RTDB query, I don't think there's much else you can do (except maybe move to Firestore, which gives you multiple filters).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.