Objective-C/iPhone issue:Core Data

doronkatz

Registered
Hi guys,

Im trying to load data with Core Data into my app and I keep getting this error: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'category''"

Basically in my appdelegate, I am doing the following:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:mad:"iKosher.sqlite"];
/*
Set up the store.
For the sake of illustration, provide a pre-populated default store.
*/
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:mad:"iKosher" ofType:mad:"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}

NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

NSError *error;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}

return persistentStoreCoordinator;
}


#pragma mark -
#pragma mark app delegate life cycle
- (void)applicationDidFinishLaunching:(UIApplication *)application {
productListController.managedObjectContext = self.managedObjectContext;
[window addSubview:rootController.view];
[window makeKeyAndVisible];
}


I have set up my managed object classes (lower case category.h, product.h) and basically the productListController is a UIViewController that sits below the rootcontroller tab. I call the rootController tab in appdelegate directly, not the productListController which is called form the rootController through IB.

Now, the app loads up but when i go to the tab for productlist:-

Now, the app loads up but when i go to the tab for productlist:-
- (void)viewDidLoad {
* * [super viewDidLoad];

* * */*
* * *Fetch existing events.
* * *Create a fetch request; find the Event entity and assign it to the request; add a sort descriptor; then execute the fetch.
* * **/

* * NSFetchRequest *request = [[NSFetchRequest alloc] init];
* * //NSEntityDescription *entity = [NSEntityDescription entityForName:mad:"Event" inManagedObjectContext:managedObjectContext];
* * NSEntityDescription *entity = [NSEntityDescription entityForName:mad:"category" inManagedObjectContext:managedObjectContext];
* * [request setEntity:entity];

* * // Execute the fetch — create a mutable copy of the result.
* * NSError *error = nil;
* * NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
* * if (mutableFetchResults == nil) {
* * * * // Handle the error.
* * }

* * [request release];

* * //NSError *error = nil;
* * if (![[self fetchedResultsController] performFetch:&error]) {
* * * * // Handle error
* * * * NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
* * * * exit(-1); *// Fail
* * }

I get this error. Im guessing somehow it doesnt find the category. But the class is there, and it is included in the import statement #import "category.h". A thing to note, the sqllite program that it calls, for some reason I think I had to put all the column names to be like ZCATEGORY, ZPRODUCT, going by what others have done in their blog, because when I had it the normal way, it gave me other errors.


Ive done some breakpoints and I can see in the ProductListController which is called from a UITab Controller, is failing at viewLoad, for if (![[self fetchedResultsController] performFetch:&error]).

Thanks! Ive been buffled for two full whole days.
 
First, pardon if I use incorrect terminology. I am a programmer by trade, but have not dabbled with Cocoa or Objective-C... but I am relatively sure I understand what the message is telling you. Let's give it a try:
"Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'category''"
This error is telling you that the compiler can't figure out where you instantiated an NSManagedObjectModel for the entity "category" (highlighted in bold).
* * NSFetchRequest *request = [[NSFetchRequest alloc] init];
* * //NSEntityDescription *entity = [NSEntityDescription entityForName:mad:"Event" inManagedObjectContext:managedObjectContext];
* * NSEntityDescription *entity = [NSEntityDescription entityForName:mad:"category" inManagedObjectContext:managedObjectContext];
* * [request setEntity:entity];
This is the specific line that the compiler is talking about (again, in bold).

Are you sure there's an entity that exists named "category"? Can you provide more information about whatever entity you're referring to with the name "category"? What is "Entity" and why is it commented out? Was it part of some sample code somewhere? Sorry if I'm way off-base here.
 
Back
Top