How can I retrieve objects in the order they were created?
Asked Answered
A

3

6

I am using parse to store my objects. When I go to retrieve objects, I get the objects in a random order it looks like. I Believe Parse isn't taking seconds into account, just minutes, and if the objects are made in the same minute it gives me the objects back in a random order.

PFQuery *query = [PFQuery queryWithClassName:@"ChatMessage"];
[query whereKey:@"alert" equalTo:myAlert];

I'm "filtering" the objects I get with a key.

I get the objects, they are all out of order though. I can attach milliseconds to this object when it is created (dateSince1970 type thing), but I don't want to have to do that. Is there a built in way to do this in Parse?

Amygdala answered 23/7, 2013 at 5:38 Comment(0)
A
37

yes, there is in-built feature provided by Parse.
you can use updatedAt or createdAt attributes of the class. and you put that in query also.

// Sorts the results in ascending order by the created date
[query orderByAscending:@"createdAt"];

// Sorts the results in descending order by the created date
[query orderByDescending:@"createdAt"];
Apparitor answered 23/7, 2013 at 5:58 Comment(1)
I don't think that works.. tried that in my code but the posts appear in random fashion..Clamp
R
5

I hope it will help you

 PFQuery *query = [PFQuery queryWithClassName:@"ChatMessage" ];
[query orderByDescending:@"createdAt"];
query.limit =10;
 [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
     //you will get all your objectsas per u created ur object in parse.
    }
 }];
Railey answered 23/7, 2013 at 5:49 Comment(0)
E
2

Parse itself saves the date it was created and updated. Maybe you would want to use the date it was created.

Look at this documentation: https://parse.com/docs/ios_guide#top/iOS

Example:

NSDate *updatedAt = gameScore.updatedAt;
NSDate *createdAt = gameScore.createdAt;

Hope this helps you...

Edit:

// Sorts the results in ascending order by the score field
[query orderByAscending:@"score"];

// Sorts the results in descending order by the score field
[query orderByDescending:@"score"];
Erlene answered 23/7, 2013 at 5:49 Comment(1)
in my case, i needed add casting, 'NSDate* objectCreatedDate = ((PFObject*)object).createdAt;' (thank you lakesh)Franke

© 2022 - 2024 — McMap. All rights reserved.