MatzeLoCal
MatzeLoCal

Reputation: 402

Is it possible to exclude the object with the latest date?

I have a Core Data Model with an Entity that has an attribute "creation Date"

What I'd like to do is to create a fetch request that has all objects but not the latest. As the content of the Core Data store changes while the user looks at the data (i.e. a UITableView) I can not set a "latest Object" and use this for the NSPredicate.

So is there a way to create a format to exclude the data set with the latest date?

Upvotes: 0

Views: 58

Answers (2)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46728

Yes, you can build your NSFetchRequest with the following settings:

  1. Sort by date, descending order
  2. Set the offset to 1

execute the fetch and it will skip the first record.

You can then load it into a NSFetchedResultsController if appropriate.

Upvotes: 3

Mundi
Mundi

Reputation: 80271

If you are displaying the results in a table view, you can manipulate the datasource to omit the last result. (You return count minus one in numberOfRowsInSection.) This should work perfectly well with a fetched results controller as well. Don't forget to also modify the delegate methods.

If you just need an array (not recommended), read into a mutable array and remove the first or last object (depending on sort order). When the data changes, just re-fetch and replace the array.

Upvotes: 1

Related Questions