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"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"iKosher" ofType"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)applicationDidFinishLaunchingUIApplication *)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"Event" inManagedObjectContext:managedObjectContext];
* * NSEntityDescription *entity = [NSEntityDescription entityForName"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.
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"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"iKosher" ofType"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)applicationDidFinishLaunchingUIApplication *)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"Event" inManagedObjectContext:managedObjectContext];
* * NSEntityDescription *entity = [NSEntityDescription entityForName"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.