我正在使用最新的SDK和使用Core Data的XCode 4.5.2开发iOS应用程序。
在UIViewController
我将有两个UITableView
,shopsList
和productsList
。我想使用核心数据,这是我第一次使用它。所以,我已经开始使用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
,不是吗?
答案 0 :(得分:3)
首先在app delegate中分配viewController的managedObjectContext。
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSFetchedResultsController *productsListFetchedResultsController;
@property (strong, nonatomic) NSFetchedResultsController *shopsListFetchedResultsController;
在数据模型中创建2个实体,并在需要时填写appdelegate中的一些可用数据。
在viewWillAppear中,您将2个实体提取到2个NSFetchedResultsController,只需在实用工具面板中拖动基本的fetchResult块代码,并将上下文更改为self.manageObjectContext,将实体名称更改为关联名称
答案 1 :(得分:0)
解决方案是使用NSFetchedResultsController
的两个实例。我的一个应用程序中有类似的设置。
我建议将获取的结果控制器定义为视图控制器的@property
。
重要:不要忘记检查FRC委托回调中调用的控制器,例如controller:didChangeObject:...
。