Listen to items added by one hundred children in Firebase

I have an application for the iOS platform where I listen to changes for every item that is sold in this market. More precisely, I listen to whether the children have changed on this subject or have left.

The problem is that I want to listen when children are added. The feature of listening when a child is added to Firebase is that you get all the siblings on this node when you first set up the listener.

Children on an item are not lists of the same item, so I cannot listen to children added after a specific timestamp. I thought of doing something like this and ignoring these initial children. It just seems such a big mess, because I, in fact, collect things from Firebase twice. Once with FEventTypeValue and once with FEventTypeChildAdded .

The elements I want to listen to are the elements that are loaded into the feed. Most likely, it will be a couple of hundreds of items, but there could potentially be thousands of items.

How would you do that? Should I just forget everything about listening to added children or is there a decent solution for this?

Edit (added sample code):

So this is how I take data for a specific item in Firebase.

 var ref = new Firebase('https://<your-Firebase>.firebaseio.com'); ref.child('listings/-KB9ZqLZBEskoUihb-yR').once('value', function(snapshot) { listing = snapshot.val(); }); 

And here I want to listen when a new child is added to this element:

 ref.child('listings/-KB9ZqLZBEskoUihb-yR').on('child_added', function(snapshot) { listing[snapshot.key()] = snapshot.val() }); 

The problem is that listening to the elements added to this node again retrieves all the child nodes, and this seems like a huge waste. How would you handle this situation?

+6
source share
1 answer

Firebase has support for getting newly added children to a specific node of course.

Here's the firebase documentation for retrieving data , and you can take a look again at the "Child Added" section. I am simply quoting important notes.

child_added is run once for each existing child, and then again every time a new child is added to the specified path

And this one

For order purposes, a second argument is also passed containing the key of the previous child.

You can also look at an example with it.

 // If we wanted to retrieve only the data on each new post added to our blogging app, we could use child_added // Get a reference to our posts var ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts"); // Retrieve new posts as they are added to our database ref.on("child_added", function(snapshot, prevChildKey) { var newPost = snapshot.val(); console.log("Author: " + newPost.author); console.log("Title: " + newPost.title); console.log("Previous Post ID: " + prevChildKey); }); 

Look carefully at prevChildKey

So, here you can do this trick by changing the structure of your database that represents here . But, I think you already have this order structure. Hope it helps.

+3
source

All Articles