Re: Core Data: combining IB & code-based filter predicates
Re: Core Data: combining IB & code-based filter predicates
- Subject: Re: Core Data: combining IB & code-based filter predicates
- From: "public.web" <email@hidden>
- Date: Wed, 7 Mar 2007 15:11:53 +0100
I think that the best way to get the compound predicate recomputed
when a subpredicate change would be to:
1 - Make each variable component of the subpredicates and the
compound predicate an ivar.
2 - use this method in your initialize handler:
[self setKeys:(NSArray *)fromDate_And_toDate_Array
triggerChangeNotificationsForDependentKey:(NSString *)
compound_predicate];
3 - Bind the "date inputs" (NSDatePickers I assume) to fromDate and
toDate
4 - Bind the filterPredicate of your NSArrayController to the
compound predicate with "validates immediately" and "continuously
updates" options set to YES.
There are certainly other ways to do this (perhaps using
NSFetchRequest) but this is how it works for me.
I have a Core Data document app and I need it to work with two or
more filters based on user input. There's the standard
NSSearchField in IB that works fine and I can also capture a user-
selected date and set that filter predicate for my data:
// This routine lets me capture the current filter (initially from
the NSSearchField)
- (NSPredicate *)filterPredicate
{
return [transactionController filterPredicate];
}
// This routine replaces the filter set by the NSSearchField; I
need a way to capture and combine them
- (void)refreshDatePredicate
{
NSExpression *lhs = [NSExpression expressionForKeyPath:@"date"];
NSExpression *greaterThanRhs = [NSExpression
expressionForConstantValue:[fromDate dateValue]];
NSPredicate *greaterThanPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs rightExpression:greaterThanRhs
modifier:NSDirectPredicateModifier
type:NSGreaterThanOrEqualToPredicateOperatorType options:0];
NSExpression *lessThanRhs = [NSExpression
expressionForConstantValue: [toDate dateValue]];
NSPredicate *lessThanPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs rightExpression:lessThanRhs
modifier:NSDirectPredicateModifier
type:NSLessThanOrEqualToPredicateOperatorType options:0];
NSPredicate *datePredicate = [NSCompoundPredicate
andPredicateWithSubpredicates:[NSArray
arrayWithObjects:greaterThanPredicate, lessThanPredicate, nil]];
[transactionController setFilterPredicate:datePredicate];
}
The problem is that only one of these is active at one time. I need
to know the best way to combine these filters. I'd love to do it
all in IB, but I don't think I can capture the date range
information and combine it there so I'm happy to do it in the code.
What's the best practice method for doing complex filter predicates
based on various user selections?
I'm really stuck behind a mental roadblock on this one so any help
is much appreciated! Thanks.
Peace,
Kevin Hoctor
email@hidden
No Thirst Software LLC
http://nothirst.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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