使用NSPredicate进行核心数据实体搜索

时间:2014-08-06 11:47:38

标签: ios core-data

我有一个应用程序将数据保存到Core Data sql,然后在tableView中查看它。我尝试添加searchBar来搜索数据。

我创建了两个NSFetchedResultsController。一个用于获取实体数据,另一个用于在使用NSPredicate过滤后获取实体数据。

- (NSFetchedResultsController *)fetchedResultsController {
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    CoreDataStack *coreDataStack = [CoreDataStack DefaultStack];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"offer" inManagedObjectContext:coreDataStack.managedObjectContext];
    [fetchRequest setEntity:entity];
    fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]];

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:@"sectionName" cacheName:nil];
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;
}

- (NSFetchedResultsController *)fetchedSearchResultsController{
    if (_fetchedSearchResultsController != nil) {
        return _fetchedSearchResultsController;
    }

    CoreDataStack *coreDataStack = [CoreDataStack DefaultStack];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"offer" inManagedObjectContext:coreDataStack.managedObjectContext];
    [fetchRequest setEntity:entity];
    fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]];    

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"location contains[c] %@", self.searchDisplayController.searchBar.text];

    [fetchRequest setPredicate:predicate];

    _fetchedSearchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:@"sectionName" cacheName:nil];
    _fetchedSearchResultsController.delegate = self;

    return _fetchedSearchResultsController;
}

cellForRowAtIndexPath显示每个单元格中的数据:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];//] forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    Entry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
    Entry *searchEntry = [self.fetchedSearchResultsController objectAtIndexPath:indexPath];

    NSString *recipe = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = searchEntry.location;
    } else {
        cell.textLabel.text = entry.location;
    }

    return cell;
}

但是我收到了这个错误:*' NSRangeException',原因:' * - [__ NSArrayM objectAtIndex:]:索引0超出空数组的范围' *

当我评论 [fetchRequest setPredicate:predicate]; 时,我不会再收到错误了。因此,我假设错误来自谓词,但我不知道如何克服它。

1 个答案:

答案 0 :(得分:0)

Entry * entry = [self.fetchedResultsController objectAtIndexPath:indexPath];     Entry * searchEntry = [self.fetchedSearchResultsController objectAtIndexPath:indexPath]; 首先检查输入和searchEntry wether是否返回null?