Paul T.
Paul T.

Reputation: 5038

NSPredicate with Core Data issue

I have Orders objects in Core Data database. Each of them have date and field flag = 0 or 1 (1 means that this object was loaded from server with all other objects and has all proper fields and it will not be changed anymore, 0 means that object can be changed later and I need to reload it from server later). I need to load that data in UITableView with NSFetchedresultController sorted by date, but I need to load this data only until 0 (because after that I will show UIActivityIndicator in the bottom of the table to load other data from server). For example, I have data sorted by date :

1 1 1 1 1 0 0 0 1 1 1 1

I need to load only first five objects (marked by bold font). I don't know how to write NSPredicate properly. If I write "flag > 0" I will load other objects with flag = 1. Right now I only see the solution to load all the data and to use for cycle to form proper array. But may there is a better solution?

Upvotes: 1

Views: 51

Answers (2)

pbasdf
pbasdf

Reputation: 21536

You could do this as two fetches: first, fetch with a predicate flag == 0, sorted by date, with a fetchLimit = 1. Get the value of the date attribute for that object (say firstDate), and use it in the predicate for the main fetch underlying the FRC (eg. "date > %@", firstDate). If you get 0 results for the first fetch, then there's no need for the predicate - all the objects must have flag == 1.

Upvotes: 1

Mundi
Mundi

Reputation: 80271

To get the top 5 objects with flag 1 sorted by date

  • Filter with NSPredicatefor the flag
  • Set the NSSortDescriptor to sort by date
  • Set the fetchLimit to 5.

Upvotes: 0

Related Questions