本文介绍了NSFetchedResultsController刷新重新获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用一个使用布尔值设置的不同谓词从NSFetchedResultsController重新获取数据。如何刷新NSFetchedResultsController以获取一组新数据?
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];
}
}
它调用
- (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;
}
推荐答案
应用程序:
// Assuming these exist
NSPredicate * predicate;
NSString * cacheName;
[[fetchedResultsController fetchRequest] setPredicate:predicate];
[NSFetchedResultsController deleteCacheWithName:cacheName];
NSError * error = nil;
[fetchedResultsController performFetch:&error];
if (error) {
// report error
}
这篇关于NSFetchedResultsController刷新重新获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!