we are using core data for our online catalog and its working fine and app available in app store now I need to upgrade the core date with some of fields and attributes. It migrated to new one but old data already stored in the app are completely vanished. I tried various ways to retain it using this code
NSString *databaseFilePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"App_iOS.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:databaseFilePath error:NULL];
NSURL *storeUrl = [NSURL fileURLWithPath: databaseFilePath];
NSError *error = nil;
if( ![[self persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration: nil URL:storeUrl options:nil error:&error])
{
[__managedObjectContext insertObjects];
}
else
{
[__managedObjectContext updatedObjects];
}
I am not get solution yet to retain the data in app. I searched in internet for this most of the face the same problem but I am not receive the good solution yet
I’ve now found out that this is quite simple – once you know where to look.#
In my AppDelegate I set-up the NSPersistentStoreCoordinator – and you need to add some options to this to tell it to handle auto-migrate:
Then you need to do in xCode:
This seems great and as simple as I wanted – but I think you need to be careful during development as you change a model – otherwise you will have to create a new version for each change.
I think what I will do is that I will keep all of the changed files and then once I get ready to deploy my update I’ll delete all the in-between files and just deploy with the oldest and latest models.