CoreData: adding lightweight migration retroactively


macrumors member
Original poster
Dec 25, 2008
Hi All,

I have an app that uses coredata as a database. The database is downloaded from the cloud so it's really not important to me to migrate the old data from one version to the next because the user can't modify the fields, just search and filter trough them.

Recently I have updated the app and added a few fields to the database. I didn't do any lightweight migration thinking that if anyway I delete the database on startup and start a new one there wouldn't be problem.

But For those users who update the app, the app crashes. So far I instructed users to delete the app and re-downloaded it from the app store. This solves the problem, but it's really not professional. And most users don't contact me, the app just crashes on startup and they let it be (because of the automatic app update feature they don't even know the problem is with the new version).

I have two questions related of how to go about and solving this:

1. since I don't need the old data (I can just download it again from the cloud) is there anyway to just completely delete the old database before the application crashes and rebuilt it again using the new structure (without doing the lightweight migration)?

2. If not, how can I do a lightweight migration retroactively? I have looked at the tutorial online, and its pretty much straight forward, but how to employ the lightweight migration from the previous version (1.2) which is no longer available on my development machine to next version I will release for the fix (1.4)? (where 1.3 is the problematic version currently on the app store that crashes when updated).

Thank you all.


May 1, 2010
It sounds like within your code you can just check to see what version of the database in being used and if it's an older version, delete it.

Maybe have a version number in the database as one of the fields, then when the app starts loading start checking the version.


macrumors member
Original poster
Dec 25, 2008
1. If I decided the take the path of the lightweightmigration, any suggestion how to go about doing it?
2. Deleting the old file is probably the easiest and safest, but How to delete the entire old database structure and use the new one?

I am using this code but it still crashes:

    NSArray *stores = [[[self managedObjectContext] persistentStoreCoordinator] persistentStores];
    NSPersistentStore *store = [stores objectAtIndex:0];
    NSError *error;
    NSURL *storeURL = store.URL;
    NSPersistentStoreCoordinator *storeCoordinator = [[self managedObjectContext] persistentStoreCoordinator];
    if ([storeCoordinator removePersistentStore:store error:&error]) {
        [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
        //recreate the store like in the  appDelegate method
        [[[self managedObjectContext] persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];