realm-swift icon indicating copy to clipboard operation
realm-swift copied to clipboard

NSPredicate that filters on the current time

Open jeremygrenier opened this issue 9 years ago • 3 comments

I'd like to have a way to create a NSPredicate (for use in Realm with Collection notifications) that filters records based on the current time.

NOT the time when the predicate was created:

NSPredicate(format: "endsAt < %@", NSDate())

But the time when the predicate is evaluated is something like this:

NSPredicate(format: "endsAt < now()")

jeremygrenier avatar Sep 05 '16 10:09 jeremygrenier

The predicate language is part of Apple's SDKs so we're not able to extend it as you suggest. It would be possible to instead provide a value that can be substituted into the predicate that Realm would recognize as being a placeholder for the current time.

bdash avatar Sep 05 '16 15:09 bdash

I don't want to extend predicate language, NSPredicate(format: "endsAt < now()") is a valid predicate for iOS8+.

But provide a value that can be substituted is a good workaround.

jeremygrenier avatar Sep 05 '16 16:09 jeremygrenier

Ah, so it is! I didn't see it mentioned when I re-skimmed Apple's Predicate Programming Guide, but now I see that it is in the list of functions in the BNF definition.

bdash avatar Sep 05 '16 16:09 bdash