2

I've got the following:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedObjContext = appDelegate.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName: "RAS")
    var error: NSError?
    let predicate1 = NSPredicate(format: "%K AND %K == false",  "rasScore", "rasStatus")

    fetchRequest.predicate = predicate1

    do {...

This gives me ALL the records that conform to the parameters of the NSPredicate, but I want ONLY the record with the highest value in rasScore. How do I go about getting that? Is it possible to include that in the NSPredicate format?

MarkHim
  • 5,686
  • 5
  • 32
  • 64
Jaco Griesel
  • 365
  • 4
  • 15
  • You want all the record but values from highest to lowest. ? It means rasScore having a highest value, that particular record will be first and having a lowest value will be last – Kiran Jasvanee Jan 28 '16 at 12:08
  • set the sort and limit to 1 result you mean? – Wain Jan 28 '16 at 12:11
  • That's what, he can set the sort using NSSortDescriptor and limit using setFetchLimit – Kiran Jasvanee Jan 28 '16 at 12:14
  • Possible duplicate of [Core Data - How to fetch an entity with max value property](http://stackoverflow.com/questions/10398019/core-data-how-to-fetch-an-entity-with-max-value-property) – Larme Jan 28 '16 at 12:23

1 Answers1

8

Set the fetchLimit of your fetchRequest to 1 and use a descriptor to sort by value of "rasScore" descending like this:

fetchRequest.fetchLimit = 1
var highestSortDescriptor : NSSortDescriptor = NSSortDescriptor(key: "rasScore", ascending: false)
fetchRequest.sortDescriptors = [highestSortDescriptor]
MarkHim
  • 5,686
  • 5
  • 32
  • 64