I am trying to load simple data from a model using Core Data and putting it into a table view. Here is the following code for my persistent store:
//AppDelegate.m
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"vofasmmmnmgd.sqlite"];
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {
NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"CoreDataSQLite" ofType:@"sqlite"]];
NSError* err = nil;
if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err]) {
NSLog(@"Oops, could copy preloaded data");
}
}
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
As you can see I am preloading data from an sqlite db. Everything works perfectly. It was able to pull data from the model and display it correctly.
BUT
Things changed when I added a second entity into the same model for the last part of the application. The app decides to crash on the "abort();" saying the following:
"The model used to open the store is incompatible with the one used to create the store";
Now this is a simple fix as I have found out because of Google research. Go to the app and delete it then that will reset the data of all the databases and let it reload. However I have done that and it still continues to not work. Here is all that I have tried with still the same result:
- Deleting the app
- Resetting content and settings on iPhone simulator
- Changing the name of the storeURL to something different other than its original value.
- Removing the store URL as listed in the comments of the code above
- Cleaning the project
I have come to the conclusion that it is because I have added another entity because when I delete the entity it runs perfectly and goes back to normal without any problems.
I have no idea what is going on which is very frustrating because I do not know how to fix this. Google is running out of solutions to this problem so I thought I would get on the wondrous Stack Overflow to get a miracle solution to end this frustration. Thanks!