Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to items added on hundred of children in Firebase

I have an iOS marketplace app, where I'm listening for changes on each item that is for sale on that marketplace. More precisely, I'm listen for whether children on that item change or gets removed.

Issue is that I want to listen for when children are added as well. The peculiar thing about listening for when a child is added on Firebase, is that you get all the siblings on that node when you first set up the listener.

The children on an item is not a lists of the same item, so I can't listen for children added after a certain timestamp. I was thinking about doing something like this and ignore those initial children. It just seems like such a big waste to, since I'm essentially fetching things from Firebase twice then. Once with FEventTypeValue and once with FEventTypeChildAdded.

The items I want to listen for are the items that are getting loaded in the feed. It would most likely be a couple of hundred of items, but could potentially be thousands of items.

How would you go about this? Should I just forget all about listening for added children or is there a decent solution for this?

Edit (added example code):

So this is how I fetch the data for a specific item on 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 for when a new child is added to that item:

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

Issue is that listening to items added on that node fetches all the child nodes again and that seems like a huge waste. How would you deal with this situation?

like image 404
Holger Sindbaek Avatar asked Apr 15 '16 19:04

Holger Sindbaek


1 Answers

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

Here's the firebase documentation for retrieving data and you might take a look at the 'Child Added' section again. I'm just quoting some important notes from there.

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

And this

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

You can take a look the at the example along with it too.

// 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);
});

Have a close look at prevChildKey

So, here you might do this trick by changing your database structure a bit which is suggested here. But, I think you already have that ordering structure. Hope it helps.

like image 158
Reaz Murshed Avatar answered Nov 12 '22 12:11

Reaz Murshed