Hi,
I'm creating an SQLite file in a Mac app using Core Data and then placing it in my iPhone app. The SQLite file is read-only, it's just mappings for states and cities. I added more cities to the file and am trying to get my iPhone app to read in the new SQLite file and use that as its peristent store.
I was hoping just replacing the SQLite file in the app would work, but it does not.
The steps I've tried are: I run the app in simulator with the old sqlite file. Exit. Replace the sqlite file with the new one in xcode. Add NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption to the addPeristentStore options. Then run the app again, but the app only shows the same data from the original file.
I've also tried versioning the model as per the Apple docs and renaming the sqlite file. But still no luck (I'm guessing because the model is still the same?).
Any ideas on how I can have the app read the newer sqlite file?
My iPhone app code:
If interested, here is the GitHub code for exporting the state - city mappings in Core Data: http://github.com/baalexander/Geography-Importer.
I'm creating an SQLite file in a Mac app using Core Data and then placing it in my iPhone app. The SQLite file is read-only, it's just mappings for states and cities. I added more cities to the file and am trying to get my iPhone app to read in the new SQLite file and use that as its peristent store.
I was hoping just replacing the SQLite file in the app would work, but it does not.
The steps I've tried are: I run the app in simulator with the old sqlite file. Exit. Replace the sqlite file with the new one in xcode. Add NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption to the addPeristentStore options. Then run the app again, but the app only shows the same data from the original file.
I've also tried versioning the model as per the Apple docs and renaming the sqlite file. But still no luck (I'm guessing because the model is still the same?).
Any ideas on how I can have the app read the newer sqlite file?
My iPhone app code:
PHP:
- (NSManagedObjectModel *)geographyObjectModel
{
if (geographyObjectModel_ == nil)
{
NSString *modelPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"Geography" ofType:@"mom"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
[self setGeographyObjectModel:managedObjectModel];
[managedObjectModel release];
}
return geographyObjectModel_;
}
//Geographical data is stored in its own persistent storage
- (NSPersistentStoreCoordinator *)geographyStoreCoordinator
{
if (geographyStoreCoordinator_ == nil)
{
NSString *storePath = [[AppDelegate applicationDocumentsDirectory] stringByAppendingPathComponent:@"Geography.sqlite"];
// Check to see if the store already exists
NSFileManager *fileManager = [NSFileManager defaultManager];
// Copy the default store if necessary
if (![fileManager fileExistsAtPath:storePath])
{
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Geography" ofType:@"sqlite"];
if (defaultStorePath)
{
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self geographyObjectModel]];
[self setGeographyStoreCoordinator:persistentStoreCoordinator];
[persistentStoreCoordinator release];
NSMutableDictionary *options = nil;
//Implement options when updating the model. Then comment options back out.
options = [NSMutableDictionary dictionary];
//Uncomment the line below to ignore version hash checks
//[options setObject:[NSNumber numberWithBool:YES] forKey:NSIgnorePersistentStoreVersioningOption];
//When migrating, uncomment the setObject lines belows
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSError *error;
if (![[self geographyStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
{
// Handle the error.
DebugLog(@"error: %@", error);
}
}
return geographyStoreCoordinator_;
}
If interested, here is the GitHub code for exporting the state - city mappings in Core Data: http://github.com/baalexander/Geography-Importer.