Oleg Sobolev
Oleg Sobolev

Reputation: 3376

Core data fetch request with predicate. Need right NSPredicate

I got entity with two NSDate properties. They are here: startTime, endTime and two restrictions, they are here too: NSDate *begin, NSDate *end

But the problem is endTime can be nil

I need to fetch all entities which:

(entity.startTime >= begin) AND (entity.endTyme == nil)

if (endType == nil)

and

(entity.startTime >= begin) AND (entity.endTyme <= end)

if (endTyme != nil)

How to make union from those two fetch predicates? Or maybe use filter?

Upvotes: 0

Views: 1258

Answers (2)

Martin R
Martin R

Reputation: 539685

(Remark: You have edited your question heavily several times. I would be better to append new information about your problem, otherwise comments and answers are difficult to understand for future readers.)

To fetch all objects where the start time is >= a given time, and the end time is nil or <= a given time, you can use the predicate

[NSPredicate predicateWithFormat:@"(startTime >= %@) AND (endTime == nil OR endTime <= %@)",
       startTime, endTime]

Upvotes: 0

Mundi
Mundi

Reputation: 80265

Your setup and predicate are correct. Check your data and you will discover that the predicate works as expected.

Upvotes: 1

Related Questions