Quiero volver a buscar datos desde mi NSFetchedResultsController utilizando un predicado diferente que se establece utilizando un valor booleano. ¿Cómo actualizo NSFetchedResultsController para recuperar un nuevo conjunto de datos?NSFetchedResultsController refresh refetch?
- (void)refreshFetchedResultsController {
NSLog(@"refreshFetchedResultsController");
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Error loading data",
@"Error loading data")
message:[NSString stringWithFormat:
NSLocalizedString(@"Error was: %@, quitting.", @"Error was: %@, quitting."),
[error localizedDescription]]
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
otherButtonTitles:nil];
[alert show];
}
}
que exige
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
NSLog(@"i was executed.");
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
TapAppDelegate *appDelegate = (TapAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Favorite" inManagedObjectContext:managedObjectContext];
NSString *sectionKey = @"favname";
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"favname" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
if(showAll == NO){
if(isXSelected == NO){
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isFirst == TRUE"];
[fetchRequest setPredicate:predicate];
}
if(isXSelected == YES){
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isFirst == FALSE"];
[fetchRequest setPredicate:predicate];
}
}
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:sectionKey
cacheName:nil];
[sortDescriptor release];
[sortDescriptors release];
frc.delegate = self;
_fetchedResultsController = frc;
[fetchRequest release];
return _fetchedResultsController;
}
He hecho esto pero cuando recuerdo la función, no ejecuta la nueva solicitud. (Mis predicados están envueltos en sentencias activadas con booleanos). –
En realidad, tienes razón. Lo siento por eso. Muchísimas gracias por la ayuda. –
@corykilger, gracias! el [NSFetchedResultsContrller deleteCacheWithName: cacheName]; era exactamente lo que necesitaba! – daveomcd