Re: Core Data NSPredicate
Re: Core Data NSPredicate
- Subject: Re: Core Data NSPredicate
- From: Indragie Karunaratne <email@hidden>
- Date: Sat, 12 Mar 2011 17:51:54 -0700
Andreas,
That predicate syntax (and block predicates) won't work in my case because I'm using them in a Core Data NSFetchRequest. I did, however, manage to solve this issue by using subpredicates:
NSPredicate *basePredicate = [NSPredicate predicateWithFormat:@"(name CONTAINS[cd] $QUERY) OR (artist.name CONTAINS[cd] $QUERY)"];
NSMutableArray *andPredicates = [NSMutableArray array];
for (NSString *term in terms) {
NSDictionary *substitution = [NSDictionary dictionaryWithObjectsAndKeys:term, @"QUERY", nil];
NSPredicate *andPredicate = [basePredicate predicateWithSubstitutionVariables:substitution];
[andPredicates addObject:andPredicate];
}
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:andPredicates];
On 2011-03-12, at 6:17 AM, Andreas Grosam wrote:
>
> On Mar 12, 2011, at 3:36 AM, Indragie Karunaratne wrote:
>
>> I have an NSManagedObject that has the following string attributes:
>>
>> artist, albumArtist, composer, comments, and lyrics
>>
>> I need to write an NSPredicate that will take an array of search terms, and check if all of the above string attributes COMBINED contains every term in the search term array. I tried the following predicate format:
>>
>> NSArray *searchTerms = [NSArray arrayWithObjects:@"testing", @"search", nil];
>> NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL %@ IN { artist, albumArtist, composer, comments, lyrics }", searchTerms];
>>
>> This triggered a parsing error and would not run. Is there any way I can do this in an NSPredicate, aside from creating a "search string" attribute in my data model containing a concatenated string of the above attributes?
>
>
> You should not include the object where the predicate is run against into the parameters of the format string. Otherwise, you would get a constant expression, rather a predicate.
>
> Furthermore, when specifying an embedded string-list you need to quote the string elements.
>
> This should work then:
>
> NSArray* terms = ...
> NSPredicate* pred = [NSPredicate predicateWithFormat:@"ALL self IN {'artist', 'albumArtist', 'composer', 'comments'}"];
>
> BOOL test = [pred evaluateWithObject:terms];
>
>
> Note: you can use a ^block for the predicate as well.
>
>
> Regards
> Andreas_______________________________________________
>
> 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