Reputation: 45921
I'm developing an iOS app with latest SDK and XCode 4.5.2 that uses Core Data.
On an UIViewController
I will have two UITableView
, shopsList
and productsList
. I want to use Core Data and this is my first time that I use it. So, I've started using Xcode Master Detail template and I have found this code on 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;
}
If I have this UIViewController
interface:
@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;
[ ... ]
If I have a shop
and a product
NSManagedObject
.
How can I could adapt - (NSFetchedResultsController *)fetchedResultsController
to work with these two UITableView
?
I will probably need to NSFetchedResultsController
, isn't it?
Upvotes: 0
Views: 354
Reputation: 80265
The solution is to go with two instances of NSFetchedResultsController
. I have a similar setup in one of my apps.
I recommend defining the fetched results controllers as @property
s of the view controller.
Important: don't forget check for which controller is called in your FRC delegate callbacks such as controller:didChangeObject:...
.
Upvotes: 0
Reputation: 126
First assign managedObjectContext of your viewController in app delegate.
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSFetchedResultsController *productsListFetchedResultsController;
@property (strong, nonatomic) NSFetchedResultsController *shopsListFetchedResultsController;
Create 2 entity in your data model, and fill some available data in appdelegate if need.
In viewWillAppear, you fetch 2 entity to 2 NSFetchedResultsController, just drag basic fetchResult block code in the utility Panel and change context to self.manageObjectContext, entity name to associate name
Upvotes: 3