6

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?

Community
  • 1
  • 1
Holger Sindbaek
  • 2,278
  • 6
  • 41
  • 68
  • 1
    There's too much text describing code and data here. Please provide the actual code and JSON (as text, no screenshot) that reproduces the situation you're asking about. See [MCVE](http://stackoverflow.com/help/mcve). – Frank van Puffelen Apr 15 '16 at 22:24
  • Is 'listings/-KB9ZqLZBEskoUihb-yR' a specific item? Are there children being added *within* that node or are you looking for other items to be added with the *listings* node. Some Firebase structure would really help us and maybe a bit of clarification of the question. – Jay Apr 17 '16 at 13:35
  • 'listings/-KB9ZqLZBEskoUihb-yR' is a specific item and I want to listen for when children gets added to that particular item. – Holger Sindbaek Apr 17 '16 at 17:54
  • Why can't you just observers for .ChildAdded, .ChildChanged & .ChildRemoved? You are correct that when a .ChildAdded listener is added, it iterates through all of the children but I would think you would want that to populate your UI. Ignore the ones that are older than the timestamp you mention and add the ones that are after. You could even set up a .childAdded query to only retrieve nodes that are later than a timestamp. Also the code you posted is a duplicate effort. Just use the .childAdded and that will read in the node in question, you don't need both. (check your syntax as well) – Jay Apr 20 '16 at 17:30
  • I'm not setting a timestamp for every child attribute on a certain node. So I can't listen for children added after a certain time! I fully understand I can listen for children added after I've fetched my child. My issue is that I'll be fetching the same attributes twice then. Does that make sense? Do you understand the case and the issue? – Holger Sindbaek Apr 20 '16 at 18:10
  • The suggestion at the end of the answer you linked too seems useful: "The alternative to this approach would be to write your new items with a priority as well, where the priority is Firebase.ServerValue.TIMESTAMP (the current server time), and then use a .startAt(...) query using the current timestamp. However, this is more complex than the approach described above." And with this approach you won't be reading everything twice. You will be first reading all exisiting children, then you will be just reading every new child as it is created – Ron Harlev Apr 20 '16 at 23:25
  • The question is unclear and we don't know the correlation between your different data sets so don't think we can can answer it. Seeing your Firebase structure (Firebase Dashboard->Export) would probably help. – Jay Apr 21 '16 at 15:11

1 Answers1

3

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.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98