0
我的核心數據搜索存在問題。加載後,我的整個表格都可見。好。我按下我的搜索按鈕,輸入一個術語,然後按搜索。顯示正確的行。好。如何在折返時查看核心數據搜索結果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, [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];
}
感謝
UPDATE:知道了,謝謝。 我只是調用這個方法:
- (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];
}
即使我的緩存設置爲零? – RyeMAC3 2011-06-16 00:38:33
當你說「新的」時,我不確定我是否跟着你? – RyeMAC3 2011-06-16 01:19:35