title: Xcode: Using a complex predicate to filter a core data set
body: <p>EDIT: I'm adding a new rough table model for comment. Is this what you had in mind?</p>
  
  <p><img src="https://i.stack.imgur.com/Q7bas.png" alt="enter image description here"></p>
  
  <p>I'm still trying to work out a search filter for a core data set. I got stuck  <a href="https://stackoverflow.com/questions/9055752/filtering-a-coredata-fetchrequest">here</a>, so I'm approaching the problem differently.</p>
  
  <p>First, what I'm trying to do is the following:</p>
  
  <ol>
  <li><p>Iterate through all objects (NSManageObject *object in tableViewModel.items)</p></li>
  <li><p>Extract all the subentity objects for each 'object' and validate against search term.</p></li>
  <li><p>If the search term does exist in the subentity, then add 'object' to 'autoSearchResults' (if it doesn't already exist there)</p></li>
  </ol>
  
  <p>My code is as follows:</p>
  
  <pre><code>NSLog(@"%s", __FUNCTION__);
  
      NSMutableArray *startArray = [NSMutableArray array];
      NSMutableArray *filteredArray = [NSMutableArray array];
  
      for (NSManagedObject *object in tableViewModel.items) 
      {
          NSLog(@"1 ");
  
          NSSet *set1 = [object valueForKeyPath:@"people.name"];
          NSString *str1 = [[set1 allObjects] componentsJoinedByString:@", "];
          peopleSet = str1;
  
          NSLog (@"peopleSet is %@ ", peopleSet);
  
          NSLog(@"2 ");
          NSSet *set2 = [object valueForKeyPath:@"place.name"];
          NSString *str2 = [[set2 allObjects] componentsJoinedByString:@", "];        
          placeSet = str2;
  
          NSLog(@"3 ");
          NSSet *set3 = [object valueForKeyPath:@"keyword.name"];
          NSString *str3 = [[set3 allObjects] componentsJoinedByString:@", "];        
          keywordSet = str3;
  
          NSLog(@"4 ");
          NSSet *set4 = [object valueForKeyPath:@"type.name"];
          NSString *str4 = [[set4 allObjects] componentsJoinedByString:@", "];        
          typeSet = str4;     
  
          NSLog(@"5 ");
          NSSet *set5 = [object valueForKeyPath:@"symbol.name"];
          NSString *str5 = [[set5 allObjects] componentsJoinedByString:@", "];        
          symbolSet = str5;
  
          NSLog(@"6 ");
  
          NSLog (@"searchText is: %@ ", searchText);
  
          myPredicate = [NSPredicate predicateWithFormat:@"(peopleSet contains[cd] %@) || (placeSet contains[cd] %@) || (keywordSet contains[cd] %@) || (typeSet contains[cd] %@) || (symbolSet contains[cd] %@)", searchText, searchText, searchText, searchText, searchText ];
  
          NSLog(@"7 ");       
          if (myPredicate) {
              [startArray addObject:object];
              NSLog (@"startArray.count is %i ", startArray.count);
          }
  
          NSLog(@"8 ");
          NSLog (@"startArray.count is %i ", startArray.count);
          /*
          if (startArray.count == 0)
          {
              NSLog(@"0");
          } else {            
              NSLog(@"9 ");
              [filteredArray addObject: startArray];
              NSLog(@"10 ");
              [startArray removeAllObjects];
              NSLog(@"11 ");
          }
          */
  
      }       
  
  
      //autoSearchResults = filteredArray;
      return autoSearchResults;
  }
  </code></pre>
  
  <p>In my testing, I have 3 objects, one with the person.name "me", one with "you", and one with  "Mother". All the other fields are blank in all objects.</p>
  
  <p>Right now, I'm stopping to see how many objects are selected for each search. Essentially, the searchText is found but ignored. That is, the routine sees that the searchText contains "me" for example, but still adds an Object where the actual text is "You".</p>
  
  <p>I would appreciate any help.. If it would help to reduce this example, please let me know..</p>