如何解决您如何在重新输入时转储核心数据搜索结果FRC以进行查看?
| 我的核心数据搜索有问题。加载后,我的整个表可见。好。我按搜索按钮,输入一个词,然后按搜索。显示正确的行。好。 问题是当我从表视图退回到顶层视图(多层表视图)并向下追溯到表时。我之前过滤的搜索结果仍会显示。如何让FRC从以前转储搜索谓词并在输入时显示所有记录? 这是我的代码的相关部分: - (void)viewDidLoad {
[super viewDidLoad];
if (managedobjectContext == nil) { managedobjectContext = [(RecipesAppDelegate *)[[UIApplication sharedApplication] delegate] managedobjectContext]; }
//search
UISearchdisplayController *searchdisplayController = [[UISearchdisplayController alloc] initWithSearchBar:sBar contentsController:self];
[self performSelector:@selector(setSearchdisplayController:) withObject:searchdisplayController];
[searchdisplayController setDelegate:self];
[searchdisplayController setSearchResultsDataSource:self];
[searchdisplayController setSearchResultsDelegate:self];
[searchdisplayController release];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@\"Unresolved error %@,%@\",error,[error userInfo]);
exit(-1);
}
self.filteredListContent = [NSMutableArray arrayWithCapacity:[[[self fetchedResultsController] fetchedobjects] count]];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (self.sBar.text !=nil)
{
nspredicate *predicate =[nspredicate predicateWithFormat:@\"name contains[cd] %@\",self.sBar.text];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@\"Unresolved error %@,[error userInfo]);
exit(-1); // Fail
}
[self.myTable reloadData];
[sBar resignFirstResponder];
}
- (NSFetchedResultsController *)fetchedResultsController {
// Set up the fetched results controller if needed.
if (fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@\"MyEntity\" inManagedobjectContext:managedobjectContext];
[fetchRequest setEntity:entity];
//set batch size
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@\"name\" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means \"no sections\".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedobjectContext:managedobjectContext sectionNameKeyPath:@\"name\" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
}
return fetchedResultsController;
}
- (void)viewDidUnload {
self.filteredListContent = nil;
self.fetchedResultsController = nil;
}
- (void)dealloc {
self.filteredListContent = nil;
self.fetchedResultsController = nil;
[fetchedResultsController release];
[managedobjectContext release];
[filteredListContent release];
[myTable release];
[super dealloc];
}
谢谢
更新:知道了,谢谢。
我只是调用此方法:
- (void)loadClean {
if(fetchedResultsController){
[fetchedResultsController release];
}
fetchedResultsController = nil;
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@\"Unresolved error %@,[error userInfo]);
exit(-1);
}
[self.myTable reloadData];
}
解决方法
您需要为FRC提供新的提取请求并删除其缓存。如果您使用相同的提取请求,您将获得相同的回报。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。