Hi Everyone,
I am working on my first Core Data app, and I am following along with the iPhoneCoreDataRecipes example. I am pretty much just looking for the functionality of being able to add/remove items in a TableView - like in the example. However I have come across a problem that I can't seem to find an answer too online.
When I am setting up the NSFetchedResultsController, my app is crashing with the error:
Quote:
|
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Gig''
|
I do have a NSManagedObject Class called Gig and my data model appears to be set up correctly. The code that I have is the following:
Header File
Code:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "GigAddViewController.h"
@class Gig;
@class GigTableViewCell;
@interface GigsListTableViewController : UIViewController <UITableViewDelegate, NSFetchedResultsControllerDelegate, GigAddDelegate> {
IBOutlet UITableView *gigsTable;
@private
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
}
@property (nonatomic, retain) UITableView *gigsTable;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
- (void)showGig:(Gig *)gig animated:(BOOL)animated;
- (void)configureCell:(GigTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
Implementation
Code:
- (void)viewDidLoad {
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];
// Set the table view's row height
self.gigsTable.rowHeight = 65.0;
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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[super viewDidLoad];
}
This is the method it is crashing in after ViewDidLoad is called.
Code:
- (NSFetchedResultsController *)fetchedResultsController {
// Set up the fetched results controller if needed.
if (fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Gig" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
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:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
}
return fetchedResultsController;
}
Any help would be greatly appreciated!