birdcage
birdcage

Reputation: 2676

Query last rows from parse.com table?

I am trying to query last 20 rows from my Parse.com table. I have followed tutorial and generated the code below. The code is returning 20 rows from the table but not last 20 rows. I returns first 20 items. How to retrieve only last 20 rows?

ParseQuery<ParseObject> query = ParseQuery.getQuery("Activities");
query.setLimit(20); 
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> scoreList, ParseException e) {
    } 
});

Upvotes: 3

Views: 2437

Answers (2)

owi7
owi7

Reputation: 11

I had the challenge to order ascending but want to get the last record anyhow. The solution is simple:

query.ascending("your col");

query.find({
      success: function(result){
      var lastRow = (result.length) - 1;
      var highestValue = result[lastRow].get("your col");
      }, error: function //... and so on

Sometimes you don't want to order descending ...

Hope I could give something back to this great forum which helps me a lot!!

Upvotes: 1

jsfrocha
jsfrocha

Reputation: 1860

It seems you would only need a different sort method on the ParseQuery you are making. You can order the query results by the CreatedDate field:

// Sorts the results in descending order by the Created Date field
query.orderByDescending("dateCreated");

So, in your code, it would be something like:

//Where "CreatedDate", set the name of the Created Date field in your table.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Activities");
query.orderByDescending("CreatedDate").setLimit(20); 
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> scoreList, ParseException e) {
    } 
});

In the end, you get also the first 20 results, but the results are ordered the other way around, so that should work as intended.

Upvotes: 6

Related Questions