Предполагая, что логика генерации правильной выборки (какой-то условный оператор) находится в получателе вашего экземпляра NSFetchedResultsController. Тогда это действительно легко
self.fetchedResultsController = nil; // this destroys the old one
[self.tableview reloadData];
// when the table view is reloaded the fetchedResultsController will be lazily recreated
Редактировать: добавление полного кода образца того, что я сделал. В основном у меня есть NSDictionary entityDescription, которые содержат значения для настройки создания NSFetchedResultsController. Если я хочу изменить fetchRequest, я изменяю свою переменную entityDescription, чтобы указать новые значения и переопределить setter, чтобы сбросить fetchedResultsController и перезагрузить таблицу. Это дает вам основную идею.
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
if (self.entityDescription == nil) {
return nil;
}
// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[self.entityDescription objectForKey:kEntityName]];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
if ([[self.entityDescription objectForKey:kEntitySortField] isEqualToString:@"null"] == NO) {
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[self.entityDescription objectForKey:kEntitySortField] ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects: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:self.moc sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
- (void)setEntityDescription:(NSDictionary *)entityDescription
{
_entityDescription = entityDescription;
self.fetchedResultsController = nil;
[self.tableView reloadData];
}
Благодарим вас за ответ. Не могли бы вы лучше объяснить, что вы имеете в виду? Приветствия. –
Я расширил свой ответ – agilityvision