为什么nspredicate不工作 [英] why nspredicate not working

查看:122
本文介绍了为什么nspredicate不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"KeyRefEntity" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"time" cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}







- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
    if (self.searchBar.text !=nil)
    {
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"ref CONTAINS[cd] %@", self.searchBar.text];
        [self.fetchedResultsController.fetchRequest setPredicate:predicate];
    }
    else
    {
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"All"];
        [self.fetchedResultsController.fetchRequest setPredicate:predicate];
    }

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);
    }

    [self.searchBar resignFirstResponder];

    [self.tableView reloadData];
}



我试图使用此代码搜索,但是 searchBarSearchButtonClicked:不工作我放置一个断点所有代码执行,但没有任何反应。

I am trying to search using this code, but code inside searchBarSearchButtonClicked: is not working I place a break point all code executes but nothing happens.

推荐答案

>不确定 [NSPredicate predicateWithFormat:@All]; 应该做,但它不太可能工作。当搜索文本更改时,您应该替换获取请求,并将新的获取请求添加到FRC。从FRC文档:

Not sure what [NSPredicate predicateWithFormat:@"All"]; is supposed to do but it's unlikely to work. When your search text changes you should replace the fetch request and add the new fetch request to the FRC. From the FRC docs:


initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:

initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:

重要提示:您不能在调用此方法后修改fetchRequest。例如,您不能更改其谓词或排序顺序。

Important: You must not modify fetchRequest after invoking this method. For example, you must not change its predicate or the sort orderings.

您的其他问题是FRC正在使用缓存。不支持在使​​用缓存时更改提取请求。一般建议是不使用缓存。如果你必须使用缓存某些原因,然后(之前)你改变获取请求,你还需要删除旧缓存 - deleteCacheWithName:)

Your other problem is that the FRC is using a cache. Changing the fetch request when using a cache is not supported. The general recommendation would be to not use a cache. If you have to use a cache for some reason then when (before) you change the fetch request you also need to delete the old cache - deleteCacheWithName:).

查看文档的FRCs 此处

See the docs for FRCs here.

这篇关于为什么nspredicate不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆