Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

iphone - 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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...