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: Kevin Hoctor <email@hidden>
- Date: Sat, 10 Mar 2007 10:23:56 -0600
On Mar 7, 2007, at 8:11 AM, public.web wrote:
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.
Thank you for this idea. Here's what I ended up doing:
- (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 *newDatePredicate = [NSCompoundPredicate
andPredicateWithSubpredicates:[NSArray
arrayWithObjects:greaterThanPredicate, lessThanPredicate, nil]];
[self setDatePredicate:newDatePredicate];
[treeController rearrangeObjects];
}
After establishing a new predicate, I tell the treeController to
update its content. Each leaf in the tree has both transactions and
filteredTransactions sets with the latter being refreshed using the
new predicate:
- (NSSet *)filteredTransactions
{
[self willChangeValueForKey:@"filteredTransaction"];
NSArray *unfilteredTransactions = [[self transactions] allObjects];
NSPredicate *combinedPredicate = [NSCompoundPredicate
andPredicateWithSubpredicates:[NSArray arrayWithObjects:[[self
currentDocument] datePredicate], [[self currentDocument]
filterPredicate], nil]];
NSArray *filteredTransactions = [unfilteredTransactions
filteredArrayUsingPredicate:combinedPredicate];
[self didChangeValueForKey:@"filteredTransaction"];
return [NSSet setWithArray:filteredTransactions];
}
The predicate from the NSSearchField in IB is retrieved as the [[self
currentDocument] filterPredicate] and combined with the date
predicate. The only thing not working in the whole process is that
the NSOutlineView is not updating its counts when I change the
NSSearchField, but does when I change the dates. It's close to happy,
I just need a way to refresh the outline view when the user types in
the search field.
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