0
所以,我對於獲取我所有的核心數據實體所調用的設置用於獲取實體謂詞fetchedResultsController最佳實踐
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController_ != nil) {
return fetchedResultsController_;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Setting" inManagedObjectContext:self.managedObjectContext_];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setResultType:NSManagedObjectResultType];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"serviceName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: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.managedObjectContext_ sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController_ = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
return fetchedResultsController_;
}
我有NSFetchedResultsController *fetchedResultsController
一個屬性,我一直在使用它therough應用程序被下面的代碼通過:
[self.fetchedResultsController_ fetchedObjects]
的問題是,我想與一個特定的屬性,即獲取實體:一個謂語:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"settingName", somesettingName];
[fetchRequest setPredicate:predicate];
問題是我應該創建一個新的fetchResultsControllerWithServiceName: (NSString *) serviceName
還是應該使用現有的方法?最佳做法是什麼?
那麼你是說創建一個方法來設置謂詞,然後再次調用performFetch? – xonegirlz 2012-03-28 02:51:49
是的,這應該就夠了。 – warrenm 2012-03-28 02:54:28
如果現在我想在調用此函數之後獲取未設置謂詞的fetchedObjects,該怎麼辦?我基本上必須重置NSFetchRequest的謂詞(self.fetchedResultsController.fetchRequest setPredicate:nil)權限後? – xonegirlz 2012-03-28 04:01:27