xCode / iOS - 需要帮助才能使核心数据搜索谓词工作

时间:2012-02-06 15:16:21

标签: xcode core-data ios5 uisearchbar

我一直在努力解决这个问题,我已经错过了整个足球赛季,并且没有看到我心爱的红皮队再次失去他们几乎所有的比赛。如果没有我的支持,即使我心爱的阿森纳也表现不佳,所以请帮忙:

我正在迭代一系列核心数据对象。我正在尝试查找主要实体和子实体包含搜索词的对象。如果他们这样做,我想将它们添加到结果数组中。

现在有数百行垃圾代码被浪费掉了,永远不会被使用,因为我遵循了不同的线程并且误导了......

到目前为止的代码是:

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

我得到的输出结果如下:

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

我不知道是否需要使用谓词,谓词是否正确。我认为我没有正确测试这种情况。

所以任何帮助都将受到高度赞赏..

编辑 - 添加一些关系模型......

enter image description here

1 个答案:

答案 0 :(得分:1)

这应该有效:

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);
      }
   }
}