Search code examples
iosswiftcore-datanspredicate

NSPredicate for selection with the given object in the middle


I need to get objects from CoreData, and I am provided with object's timestamp (which is object's property of type Date). The thing is, the object I have has to be in the center of this selection.

For example: we have 100 objects in CoreData. I have a timestamp of 45th object. I need to select 30 objects from CoreData and the 45th object should be in the center of this selection. So I need to get objects from 30th to 61s.

I came up with this variant:

let predicate = NSPredicate(format: "timestamp > %@ AND timestamp < %@", argumentArray: [baseTimestamp, baseTimestamp])

But, obviously, this won't work.

Is there any chance of getting objects by their rownum how we do in SQL? Or any other ways to achieve this selection?


Solution

  • Eventually, I ended up with 2 separate fetches, since there's no way to achieve this selection with NSPredicate :( (feel free to leave your own answer if I'm wrong, I'll gladly mark it as a right one)