Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - How to sort the data by newly added child (Swift)? [duplicate]

I'm trying to test out Firebase to allow users to post comments using push. I want to display the data I retrieve with the following;

fbl.child('sell').limit(20).on("value", function(fbdata) { 
  // handle data display here
}

The problem is the data is returned in order of oldest to newest - I want it in reversed order. Can Firebase do this?

like image 745
user2912591 Avatar asked Dec 03 '25 16:12

user2912591


2 Answers

Since this answer was written, Firebase has added a feature that allows ordering by any child or by value. So there are now four ways to order data: by key, by value, by priority, or by the value of any named child. See this blog post that introduces the new ordering capabilities.

The basic approaches remain the same though:

1. Add a child property with the inverted timestamp and then order on that.

2. Read the children in ascending order and then invert them on the client.

Firebase supports retrieving child nodes of a collection in two ways:

  • by name
  • by priority

What you're getting now is by name, which happens to be chronological. That's no coincidence btw: when you push an item into a collection, the name is generated to ensure the children are ordered in this way. To quote the Firebase documentation for push:

The unique name generated by push() is prefixed with a client-generated timestamp so that the resulting list will be chronologically-sorted.

The Firebase guide on ordered data has this to say on the topic:

How Data is Ordered

By default, children at a Firebase node are sorted lexicographically by name. Using push() can generate child names that naturally sort chronologically, but many applications require their data to be sorted in other ways. Firebase lets developers specify the ordering of items in a list by specifying a custom priority for each item.

The simplest way to get the behavior you want is to also specify an always-decreasing priority when you add the item:

var ref = new Firebase('https://your.firebaseio.com/sell');
var item = ref.push();
item.setWithPriority(yourObject, 0 - Date.now());

Update

You'll also have to retrieve the children differently:

fbl.child('sell').startAt().limitToLast(20).on('child_added', function(fbdata) {
  console.log(fbdata.exportVal());
})

In my test using on('child_added' ensures that the last few children added are returned in reverse chronological order. Using on('value' on the other hand, returns them in the order of their name.

Be sure to read the section "Reading ordered data", which explains the usage of the child_* events to retrieve (ordered) children.

A bin to demonstrate this: http://jsbin.com/nonawe/3/watch?js,console

like image 103
Frank van Puffelen Avatar answered Dec 06 '25 05:12

Frank van Puffelen


Since firebase 2.0.x you can use limitLast() to achieve that:

fbl.child('sell').orderByValue().limitLast(20).on("value", function(fbdataSnapshot) { 
  // fbdataSnapshot is returned in the ascending order
  // you will still need to order these 20 items in
  // in a descending order
}

Here's a link to the announcement: More querying capabilities in Firebase

like image 26
Maksymilian Majer Avatar answered Dec 06 '25 05:12

Maksymilian Majer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!