Replacing a SQLite Core Data File in an Application

I create a SQLite file on Mac using Core Data, and then place it in my iPhone application ( GitHub project , if you're interested), I want to release an update for the application with the updated SQLite file. The model will be the same objects and attributes, there will simply be more data.

I run the application in a simulator with an old sqlite file. Exit. Replace sqlite file with new one in xcode. Add NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption in the addPeristentStore option. Then run the application again, but the application only shows the same data from the source file.

I also tried the model version according to the Apple docs and this SO post and renamed the sqlite file. But still no luck (I guess, because the model remains the same?).

Any ideas on how the application can read the new sqlite file?

+3
source share
1 answer

The My Core Data iPhone application uses the standard code provided when creating the project and Use Basic Data for Storage. It will switch the SQLite database, which will be used when I change the value of the file name constant "DB_NAME" and restore:

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent:DB_NAME]];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
+3
source

Source: https://habr.com/ru/post/1719577/


All Articles