Re: filtering an array of entities using NSPredicate
Re: filtering an array of entities using NSPredicate
- Subject: Re: filtering an array of entities using NSPredicate
- From: Koen van der Drift <email@hidden>
- Date: Mon, 15 Oct 2012 20:59:26 -0400
I think this is the solution:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName: @"Person" inManagedObjectContext: context]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"name IN %@", [persons valueForKey:@"name"]]];
[fetchRequest setResultType: NSDictionaryResultType];
NSError *error;
NSArray *personsMatchingNames = [context executeFetchRequest: fetchRequest error: &error];
NSMutableArray *uniqueNames = [NSMutableArray arrayWithArray: persons];
[uniqueNames removeObjectsInArray: personsMatchingNames];
I also had to set the resulttype of the fetchrequest to NSDictionaryResultType.
Thanks all for the help.
- Koen.
On Oct 15, 2012, at 11:51 AM, Keary Suska <email@hidden> wrote:
>
> On Oct 15, 2012, at 6:23 AM, Koen van der Drift wrote:
>
>> On Mon, Oct 15, 2012 at 8:19 AM, Jerry Krinock <email@hidden> wrote:
>>>
>>> On 2012 Oct 15, at 03:45, Koen van der Drift <email@hidden> wrote:
>>>
>>>> So I cannot directly filter an array of dictionaries using a predicate? That was the whole goal of my question, see my original post.
>>>
>>> Oh, now I see. I'd assumed that 'persons' was an array. It's a dictionary!
>>
>> Even better, it's an array of dictionaries! :-)
>
> Willeke probably addressed your issue best. The contents of the collection that you pass to the aggregate expression must be the same type of value as the left hand expression. So, to have "name IN {ARRAY}", ARRAY *must* be an array of "name"s. In most cases, that will be strings. When you pass dictionaries, how is the predicate to know what value to use from the dictionary? There is no magic here. Other than how curiously the NSPredicate interpreted the array of dictionaries. Interesting, but not useful to your purpose. In any case, the predicate that you have shown is not valid for your case.
>
> Have you tried Willeke's suggestion? If it doesn't work, it doesn't mean that the predicate is bad, as -predicateWithFormat: is notorious for not working well with aggregate operations. Jerry Krinock's suggestion of constructing the predicate manually works most reliably, so that would be your next step to a solution.
>
> HTH,
>
> Keary Suska
> Esoteritech, Inc.
> "Demystifying technology for your home or business"
>
_______________________________________________
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