why nspredicate not working
- (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:
内的searchBarSearchButtonClicked:
不工作我放置一个断点所有代码执行但没有任何反应。
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.
链接地址: http://www.djcxy.com/p/60640.html上一篇: Eclipse跳转到右大括号
下一篇: 为什么nspredicate不工作