我正在使用最新的 SDK 和 XCode 4.5.2 开发一个使用 Core Data 的 iOS 应用程序。
在一个UIViewController
我将有两个UITableView
,shopsList
和productsList
. 我想使用Core Data,这是我第一次使用它。因此,我开始使用 Xcode Master Detail 模板,并且在以下位置找到了这段代码MasterViewController.m
:
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[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:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
如果我有这个UIViewController
界面:
@interface FirstViewController : UIViewController<CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
{
CLLocationManager* locationManager;
BOOL isMenuHidden;
BOOL isShopsListOpen;
BOOL isProductsListOpen;
long int selectedShopRow;
long int selectedProductRow;
NSIndexPath* shopCheckedIndexPath;
NSIndexPath* productCheckedIndexPath;
}
@property (unsafe_unretained, nonatomic) IBOutlet UITableView *shopsList;
@property (unsafe_unretained, nonatomic) IBOutlet UITableView *productsList;
[ ... ]
如果我有一个shop
和一个product
NSManagedObject
。
我怎样才能适应- (NSFetchedResultsController *)fetchedResultsController
这两个工作UITableView
?
我可能需要NSFetchedResultsController
,不是吗?