title: Combining 'AND' and 'OR' Condition in NSPredicate
body: <p>Back again needing more help with constructing my NSPredicates :(</p>
  
  <pre><code>Category
  {
     name:string
     subs&lt;--&gt;&gt;SubCategory
  }
  
  SubCategory
  {
     name:string
     numbervalue:NSNumber
  }
  </code></pre>
  
  <p>I would like to know how to create a predicate with <strong>AND</strong> and <strong>OR</strong>.</p>
  
  <p>For example, I would like to return every Category with name == "category_name" that also has a subcategory with a numbervalue of "valueA" <strong>OR</strong> "valueB".</p>
  
  <p>I've tried every possible combination of predicate constructors I could come up with but I just cant get it to work.</p>
  
  <p>This is my best attempt so far.</p>
  
  <pre><code>NSPredicate *predicate=[NSPredicate predicateWithFormat@"(name== %@) AND (ANY subs.numbervalue==%@ OR ANY subs.numbervalue==%@)", @"aname", value1, value2];
  </code></pre>
  
  <p><strong>Edit 1</strong></p>
  
  <p>Second attempt. The filtering on the 'numbervalue' is still not working.</p>
  
  <pre><code>NSNumber valueA=[NSNumber numberWithInt:20];
  NSNumber valueA=[NSNumber numberWithInt:90];
  NSArray *arr=[NSArray arrayWithObject:valueA,valueB,nil];
  
  predicate=[NSPredicate predicateWithFormat:@"(name==%@)AND(ANY subs.numbervalue IN %@)",@"aname",arr];
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>I've tried filtering just by numberValue and have left the name out alltogether.</p>
  
  <p>1) using this results in the entire set being returned even if only 1 item in the set has the value. </p>
  
  <pre><code>predicate=[NSPredicate predicateWithFormat:@"(ANY subs.numbervalue IN %@)",arr];
  </code></pre>
  
  <p>2) Using this results in the same problem, every item in the set being returned even if only 1 of them matches.</p>
  
  <pre><code>predicate=[NSPredicate predicateWithFormat:@"((SUBQUERY(subs, $x, $x.numbervalue == %@ or $x.numbervalue == %@).@count &gt; 0)", value1, value2];
  </code></pre>
  
  <p>Also using the simplest version results in the same problem.... I didnt notice this earlier.</p>
  
  <pre><code>NSPredicate *predicate=[NSPredicate predicateWithFormat@"(ANY subs.numbervalue==%@ ,valueA];
  </code></pre>
  
  <p>So I think I have my problem narrowed down. </p>
  
  <p>Category is an Entity.
  SubCategory is an Entity.</p>
  
  <p>Category has a one to many relationship with SubCategory and is represented as an NSSet of SubCategory.</p>
  
  <p>Each SubCategory has an attribute named numbervalue.</p>
  
  <p><strong>Edit 3</strong></p>
  
  <p>To test I have 2 Categorys.
  Both Categories have 10 subs, each with a numbervalue of 1 -10;</p>
  
  <p>Using this code both categories are returned along with all 10 subs for each.</p>
  
  <p>Expected result is both categories returned , each with just 2 subs.</p>
  
  <p>I've traced out all my objects and the data is correct. The problem is I cant return a category that has filtered subs.</p>
  
  <pre><code>NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSManagedObjectContext *context=[[DataSource sharedDataSource]managedObjectContext];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context];
  [fetchRequest setEntity:entity];
  
  [fetchRequest setFetchBatchSize:20];
  
  NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
  NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
  
  NSPredicate *predicate;
  NSNumber *a=[NSNumber numberWithFloat:1];
  NSNumber *b=[NSNumber numberWithFloat:2];
  predicate=[NSPredicate predicateWithFormat:@"(SUBQUERY(subs,$x,$x.numbervalue==%@ or $x.numbervalue==%@).@count&gt; 0)",a,b];
  
  [fetchRequest setPredicate:predicate];
  [fetchRequest setSortDescriptors:sortDescriptors];
  
  NSError *error = nil;
  [NSFetchedResultsContoller deleteCacheWithName:nil];
  
  NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"name" cacheName:@"Master"];
  aFetchedResultsController.delegate = self;
  
  self.fetchedResultsController = aFetchedResultsController;
  
  if (![self.fetchedResultsController performFetch:&amp;error]) {
  
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
     abort();
  }
  </code></pre>