Re: Archiving NSBlockPredicate and NSPredicate date functions
Re: Archiving NSBlockPredicate and NSPredicate date functions
- Subject: Re: Archiving NSBlockPredicate and NSPredicate date functions
- From: Dave DeLong <email@hidden>
- Date: Tue, 11 May 2010 16:01:58 -0600
Since no one had any suggestions...
I ended up doing some interesting stuff with this. When constructing my predicate, I turn the offset into a substitution variable. So in the example I gave, "NOW() - 1 day" would become the variable "$NOW86400". Then any time I need to evaluate that predicate, I first run it through an NSPredicate category searches the predicate format string for anything matching \$NOW(\d+). I retrieve the offset via a capture group and use that to create a substitution value for the variable (ie, $NOW86400 becomes [NSDate dateWithTimeIntervalSinceNow:-86400]), which I then resolve via the regular predicateWithSubstitutionVariables: method.
It's not the most elegant solution, but it works and it does what I need. :)
As a side note, I want to shake the hand of the guy who wrote NSPredicate and friends. They're really awesome! :D
Cheers,
Dave
On Apr 28, 2010, at 2:13 PM, Dave DeLong wrote:
> Hi everyone,
>
> I've figured out a way to do an "date in the last X days" predicate, but I'm having to do it as a block predicate. This works fine, but I've run up against another situation: NSBlockPredicates don't support archiving. This is a problem, since I need to store this predicate in a CoreData store.
>
> Basically, I'm trying to do have a predicate like:
>
> NSPredicate * p = [NSPredicate predicateWithFormat:@"%@ >= NOW() - 86400", aDate];
>
> If that actually worked, it would return true if "aDate" is newer than 1 day ago (86400 seconds = 1 day).
>
> I can do this with a block predicate:
>
> NSTimeInterval timeAgo = -86400;
> NSPredicate * p = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary * bindings) {
> NSDate * cutoff = [NSDate dateWithTimeIntervalSinceNow:timeAgo];
> return ([cutoff laterDate:evaluatedObject] == evaluatedObject);
> }];
>
> ....except that this can't be archived.
>
> Is there a way around this? I'm not aware of any function like "NOW()" that can be used in an NSPredicate.
>
> The only other way I've thought of is to use a substitution variable, something like $NOW, and then create my predicate dynamically using -predicateWithSubstitutionVariables:. This could work, except that some of my predicates get evaluated very frequently, and I'm worried about that being too costly. (However, if it's the only alternative, then I have no choice).
>
> Thoughts? Suggestions?
>
> Thanks!
>
> Dave_______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden