cocoanspredicatenspredicateeditor

NSPredicateEditor, ignore rows with no search terms?


I'll say right off I am an inexperienced Cocoa programmer, and I apologize if my question is answered in the docs somehow and I merely missed it, or there was something I don't understand in how the NSPredicateEditor works. However, I did attempt to search in the docs and googled, to little effect. Thus, I bring the question to you.

I am attempting to filter a Core Data-based table view with an NSPredicateEditor. The filtering works fine, in terms of the rows filtering based on various criteria, through creating an NSCompoundPredicate. However, when a row in the Predicate Editor has no text in its search field, the NSPredicate that is returned is something along the lines of

dataName CONTAINS[cd] ""

...which matches none of the Core Data records -- not what I want. If that search field in that row is blank, essentially I don't want it to filter my data set at all.

My question then is, what is the easiest/best solution to ignore these parts of the predicate? My initial idea was to parse the NSPredicateEditor value row-by-row with (NSPredicate *)predicateForRow:(NSInteger)row and rebuild the predicate myself, ignoring lines which are trying to match "", but that seems unnecessarily cumbersome. Taking the final NSCompoundPredicate apart with - (NSArray *)subpredicates and editing it that way also seems like perhaps I'm taking the wrong tack. Is there a more elegant way to do this?

*To be clear, I'm thinking of editing the copy of the predicate which I then pass to the controller for my table view, not actually editing the initial predicate that is currently stored in the NSPredicateEditor.


Solution

  • It looks like you should be able to subclass NSPredicateEditor and override predicateForRow: (declared in NSPredicateEditor's superclass, NSRuleEditor) and do your substitution there. You would basically just called super's implementation, then examine the predicate and return a newly created one if necessary.