I’m running latest XCode (4.5.1) and ios simulator 5.1.
So here’s the problem, I have two entities in one core data model, and two tableviewcontrollers representing them. I’d like to set up two NSFetchedResultsControllers (one per tableviewcontroller) wich both work on the same context, but on different entities. When I create the first one, everything’s just fine, but creating the second forces EXC_BAD_ACCESS in the initWithFetchRequest: method.
I can provide the code used, but I beleive it’s not neccessary here. Basically, is that possible and if it is not, then I’d like to see a better solution to my problem.
EDIT: code:
showController.context = managedObjectContext;
self.showcaseController = [[UINavigationController alloc]initWithRootViewController:showController];
self.showcaseController.tabBarItem.title = @"Витрина";
categoryController = [[UMCategoriesController alloc] initWithNibName:@"UMCategoriesController" bundle:nil];
categoryController.context = managedObjectContext;
self.categoriesController = [[UINavigationController alloc]initWithRootViewController:categoryController];
So basically I have a UITabBarController filled with two UINavigationControllers, each starts with UIViewController with UITableView in each. I just set NSManagedObjectContext created in AppDelegate.
This is UMShowcaseController.m NSFetchedResultsController’s setter. When I use NSFetchedResultsController only here, this works perfectly.
-(NSFetchedResultsController *) frc
{
if (_frc != nil)
{
return _frc;
}
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Goods" inManagedObjectContext:context]];
_frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
_frc.delegate = self;
NSError __autoreleasing *error = nil;
if (![_frc performFetch:&error]) {
}
return _frc;
}
But, when I add NSFetchedResultsController to the second UIViewController, UMCategoriesView – it crashes.
if (_frc != nil)
{
return _frc;
}
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Categories" inManagedObjectContext:context]];
_frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; //here it fails
_frc.delegate = self;
NSError __autoreleasing *error = nil;
if (![_frc performFetch:&error]) {
}
return _frc;
Damn I am.
The only thing here’s missing is the sort descriptor in the second NSFetchedResultsController.
After re-reading the documentation everything works fine.
Read the documentation and don’t forget to set sort descriptors.
Martin, thank you for your comment, it’s you who made me re-reading the documentation.