Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchedResultsController refresh refetch?

I want to refetch data from my NSFetchedResultsController using a different predicate which is set using a boolean value. How do I refresh NSFetchedResultsController to fetch a new set of data?

- (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];
}

}

which calls

- (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;
}
like image 841
Oh Danny Boy Avatar asked Aug 03 '10 17:08

Oh Danny Boy


2 Answers

Here's how we do it in an application:

// Assuming these exist
NSPredicate * predicate;
NSString * cacheName;

[[fetchedResultsController fetchRequest] setPredicate:predicate];
[NSFetchedResultsController deleteCacheWithName:cacheName];

NSError * error = nil;
[fetchedResultsController performFetch:&error];
if (error) {
    // report error
}
like image 63
Cory Kilger Avatar answered Nov 05 '22 11:11

Cory Kilger


Don't forget to set fetchedResultsController = nil before performFetch. Otherwise it will use the old one.

like image 8
Unibrium Avatar answered Nov 05 '22 10:11

Unibrium