Re: Core Data Predicate Question
Re: Core Data Predicate Question
- Subject: Re: Core Data Predicate Question
- From: The Karl Adam <email@hidden>
- Date: Fri, 08 Jul 2011 13:40:33 -0700
Well, when I said ANY, I really meant "IN termsArray", but for the
partial matches that you want your approach is fine, you'll just want
to use > & <= with character evaluations to speed this up. Check the
2010 CoreData Performance session from WWDC for more details, but it's
a standard SQL trick to speed up string comparisons.
_Karl
On Thu, Jul 7, 2011 at 10:38 AM, Indragie Karunaratne
<email@hidden> wrote:
> I came up with this code earlier:
> NSArray *searchTerms = [cleanedQuery componentsSeparatedByString:@" "];
> NSPredicate *basePredicate = [NSPredicate
> predicateWithFormat:@"SUBQUERY(keywords, $keyword, $keyword.name BEGINSWITH
> $QUERY).@count != 0"];
> NSMutableArray *subpredicates = [NSMutableArray array];
> for (NSString *searchTerm in searchTerms) {
> NSDictionary *sub = [NSDictionary
> dictionaryWithObjectsAndKeys:searchTerm, @"QUERY", nil];
> NSPredicate *termPredicate = [basePredicate
> predicateWithSubstitutionVariables:sub];
> [subpredicates addObject:termPredicate];
> }
> NSPredicate *combinedPredicate = [NSCompoundPredicate
> andPredicateWithSubpredicates:subpredicates];
>
> This doesn't use "ANY" as you mentioned (not sure exactly how I would use
> that) but it works. However, I'm sure there's a better way to simplify this
> instead of using a giant compound AND predicate to match each one of the
> search terms. Is there any way to simplify this?
> Thanks
> On 2011-07-07, at 11:23 AM, The Karl Adam wrote:
>
> You want to be using SUBQUERY() to match ANY keywords. Check the docs
> for the predicate reference details for this.
>
> _Karl
>
> On Wed, Jul 6, 2011 at 8:23 PM, Indragie Karunaratne
> <email@hidden> wrote:
>
> Hi all,
>
> I have a Core Data object model that I'm trying to write a fetch predicate
> for (to use for search). Quick explanation of the model:
>
> We'll call the main entity "Book". There's also a "Keyword" entity. The
> Book entity has a to-many relationship with the Keyword entity called
> "keywords". In turn, the Keyword entity has an inverse relationship with the
> Book entity called "book". The Keyword entity has a single attribute called
> "name". So basically, each Book has Keywords that describe it.
>
> For my search, I have an array of search terms. I need a predicate that I
> can use on fetch requests for the Book entity that will evaluate to TRUE if
> ALL of the search terms have a corresponding Keyword in that the "name"
> property begins with the search term.
>
> For example:
>
> There are three books:
>
> Book1 - keywords: {"fiction", "scifi"}
>
> Book2 - keywords: {"nonfiction"}
>
> If the search terms were {"fic", "nonfic", "sci"} the resulting fetched
> array would contain NOTHING because none of the books have keywords that
> begin with all 3 of those search terms..
>
> However, if the search terms were {"fic", "sci"}, the resulting fetched
> array would contain Book1 since its keywords "fiction" and "scifi" begin
> with the two search terms "fic" and "sci". The key part here is that ALL of
> the search terms have to have a corresponding keyword as demonstrated above
> for the predicate to evaluate to true.
>
> I hope I've explained this problem well enough, it's hard to put this stuff
> into words ;-)
>
> Any help is appreciated,
>
> Indragie_______________________________________________
>
> 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
>
>
>
_______________________________________________
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