Atma
Atma

Reputation: 29767

how to get most recently added parse.com object

Is there any way to do a query in Parse to get the most recently added PFObject type?

I know that I can do a query with a greater than criteria, but it would be very useful if there was a function to get the most recent object added when no date is known.

Upvotes: 12

Views: 6655

Answers (2)

Eric Qian
Eric Qian

Reputation: 2256

Just add a descending order on createdAt and get the first object:

PFQuery *query = [PFQuery queryWithClassName:@"YourClassName"];
[query orderByDescending:@"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    // code
}];

In Swift:

var query = PFQuery(className: "YourClassName")
query.orderByDescending("createdAt")
query.getFirstObjectInBackgroundWithBlock {(object: PFObject?, error: NSError?) -> Void in
    // code
}

Upvotes: 33

drew..
drew..

Reputation: 3604

This may not address the use case in question. I often want to get back the objectId of the object i just added in code. While it might be automatically possible, i have missed any info in this regard. So i usually have my own unique identifier that i add, and then retrieve the object by that identifier in the async block.. thus getting the objectId..

Upvotes: 0

Related Questions