why nspredicate not working

走远了吗. 提交于 2019-12-12 02:57:10

问题


- (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];
}

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.


回答1:


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:

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

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:).

See the docs for FRCs here.



来源:https://stackoverflow.com/questions/16482239/why-nspredicate-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!