user1831389
user1831389

Reputation: 201

Equivalent of SQL ORDER BY in NSPredicate - CoreData

What would be the NSPredicate format equivalent to the following SQL query?

SELECT * FROM tableName ORDER BY date DESC LIMIT 1

I am trying this

let predicate = NSPredicate(format: "ORDER BY date LIMIT 1")

but the error is "Unable to parse the format string"

Upvotes: 3

Views: 2673

Answers (1)

user4151918
user4151918

Reputation:

A predicate is used for filtering. No filtering here, no predicate needed.

You want to sort your fetched results, which is handled by NSFetchRequest.

To order your results by date, descending

fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]

To limit your results

fetchRequest.fetchLimit = 1

Upvotes: 9

Related Questions