Search code examples
xcodecore-dataios5uisearchbar

xCode / iOS - Help needed making a core data search predicate work


I've been working on this problem for so long, I've missed the entire football season and not seen my beloved Redskins lose almost all their games again. Even my beloved Arsenal are not doing well without my support, so please help:

I am iterating through a series of core data objects. I'm trying to find objects where the main and sub-entities contain a search term. If they do, I want to add them to a results array.

There are hundreds of lines of junk code now are wasted and will never be used because the different threads I've followed and false leads taken..

The code so far is this:

NSLog(@"%s", __FUNCTION__);

NSMutableArray *tempArray = [NSMutableArray array];

for (NSManagedObject *object in tableViewModel.items) {
    //myPredicate = [NSPredicate predicateWithFormat:@"(people.name contains[cd] %@)", searchText];
    //another one for the junk heap

    NSLog(@"searchText: %@", searchText); //this is ok
    NSLog(@"\n\nobject is: %@\n\n", object); //this is ok

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(people.name contains[cd] %@)", searchText];

    BOOL ok = [predicate evaluateWithObject:object]; // this is not ok
    if (ok) {
        NSLog (@"result is TRUE");
        [tempArray addObject:object];
        NSLog(@"\n\ntempArray: %@\n\n", tempArray);
    } else {
        NSLog (@"result is FALSE");
    }
}

NSLog(@"out of records");
autoSearchResults = tempArray;
NSLog(@"\n\nautoSearchResults: %@\n\n", autoSearchResults);

return autoSearchResults; // return my own search array

What I get as output is as follows:

object is: <MainEntity: 0x79c51cb0> (entity: MainEntity; id: 0x79c50880 <x-coredata://99B12EAD-002C-402C-99CA-CF5A328E67E5/MainEntity/p2> ; data: {
    audioName = 00010206120609;
    audioNo = 0;
    date = "2012-02-06 11:10:00 +0000";
    Main = "Main-0001 12.02.06";
    interpret = nil;
    keyword = "<relationship fault: 0x79c708a0 'keyword'>";
    order = 0;
    people = "<relationship fault: 0x79c708c0 'people'>";
    place = "<relationship fault: 0x79c710b0 'place'>";
    recurring = 0;
    summary = Test1;
    symbol = "<relationship fault: 0x79c71110 'symbol'>";
    type = "<relationship fault: 0x79c71170 'type'>";
})

2012-02-06 10:00:11.454 Appkeeper[42598:16c03] result is FALSE

I don't know if I need to use a predicate, whether the predicate is correct. I do not think that I'm testing the condition correctly.

So any help will be greatly appreciated..

EDIT - Adding a bit of the relationship model....

enter image description here


Solution

  • This should work:

    for (NSManagedObject *object in tableViewModel.items) {
       for (NSManagedObject *person in object.people) {
          if (person.name && 
              [person.name rangeOfString:searchTerm 
                                 options:NSCaseInsensitiveSearch]) {
             NSLog(@"%@ contains %@.", person.name, searchTerm);
          }
          else {
             NSLog(@"%@ does not contain %@.", person.name, searchTerm);
          }
       }
    }