1

I'm trying to find an object matching a string and a set of objects. My predicate looks like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@ and individuals CONTAINS %@", name, individuals];

I'm not getting any hits. Although I know there is an entity matching the name and individuals set.

What is wrong with my predicate?

EDIT: I've managed to do some progress. The problem now is if I try to find a group which has an existing group name and existing contacts ie groupname = "test" and individuals.name = "john doe" and individuals.contactInfo = "123" it will find me this group correctly but if I have a group with the same groupname and same contact + another contact it will also find me this group which I do not want.

I only want the group which matches exactly the predicate.

I'm now using subpredicates by doing this:

NSMutableArray *subPredicates = [NSMutableArray initWithCapacity:5];
// Add group name to predicate
[subPredicates addObject:[NSPredicate predicateWithFormat:@"name == %@", name]];

for (NSDictionary *contactInfo in individuals) {

    NSString *name = [contactDict objectForKey:@"name"];
    NSString *contactInfo = [contactDict objectForKey:@"contactInfo"];

    NSPredicate *individualPredicate = [NSPredicate predicateWithFormat:@"ANY individuals.name LIKE %@ AND any individuals.contactInfo LIKE %@", name, contactInfo];

    [subPredicates addObject:individualPredicate];
}

NSPredicate *individualsPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];

Group *group = // do the fetch with the predicate
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193

2 Answers2

3

A set of objects cannot be matched with the CONTAINS predicate. You can match one object in a set using the ANY predicate:

[NSPredicate predicateWithFormat:@"name == %@ and ANY individuals.name == %@", name, individualName]; 
diederikh
  • 25,221
  • 5
  • 36
  • 49
1

I am typing this on the phone and cannot verify it, but the following predicate might work for you:

[NSPredicate predicateWithFormat:@"name == %@ AND SUBQUERY(individuals, $x, $x IN %@).@count == %d", 
    name, individuals, individuals.count];

It searches for objects with the given name whose individuals are a superset of the given set.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • I thought SUBQUERY can be used to filter out a collections based on a predicate. Apple's example: (SUBQUERY(residents, $x, $x.firstname == "Jane" && $x.lastname == "Doe").@count != 0). I don't think it will work on given list of elements. – diederikh Oct 29 '12 at 23:12
  • This predicate didn't work for me, if I have a group containing two persons this predicate will return this group even though I have supplied just one of the persons in this group. – Peter Warbo Oct 30 '12 at 14:21
  • I think I found the exact answer to my question in another thread that you had answered so I'll mark this answer as accepted. (answer here: http://stackoverflow.com/a/13086353/294661) – Peter Warbo Oct 30 '12 at 14:59
  • @PeterWarbo: "if I have a group containing two persons this predicate will return this group even though I have supplied just one of the persons in this group" - I thought that this is what you wanted, since your initial example used "CONTAINS" in the predicate. But I am glad that my answers helped you. – Martin R Oct 30 '12 at 17:17