gpt4 book ai didi

ios - 如何在重新进入查看时转储核心数据搜索结果 FRC?

转载 作者:行者123 更新时间:2023-11-29 05:03:15 26 4
gpt4 key购买 nike

我的核心数据搜索出现问题。加载后,我的整个表格都是可见的。好的。我按下搜索按钮,输入一个术语,然后按下搜索。显示正确的行。好的。

问题是当我从 TableView 返回到顶级 View (多级 TableView )并向下钻取到表时。我之前过滤的搜索结果仍然显示。如何让 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, [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, [error userInfo]);
exit(-1);
}

[self.myTable reloadData];
}

最佳答案

您需要为 FRC 提供新的获取请求并删除其缓存。如果您使用相同的获取请求,您将获得相同的返回。

关于ios - 如何在重新进入查看时转储核心数据搜索结果 FRC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6337992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com