Coredata after Application Update

那年仲夏 提交于 2020-02-05 05:52:30

问题


I'm trying to implement a process for my application that makes the posibility to update the app when needed, and with update I mean donwnload the newer application .ipa in the device.

The thing is that I'm using CoreData to store the server data brought at the first launch, and between the old version and the newer one I've added some entities and atributes for some old entities to the DB. That makes conflicts, as I have no idea how to handle migrations and/or any thing that can provide me the ability to re-create the data base as the structure has changed.

For now, if I update an application with the same DB structure, the app works ok, but if I modify it the app crashes, as expected.

Any thoughts?


回答1:


If the data inside the Application can be recreated/downloaded from server, there is a great solution. As I understood You are getting data from server and this is the wonderful case, that means the old data can bee recreated in new database. You don't need to setup migration stack, there is a very quick solution. The trick is to delete the old sqlite database and create a new one.

Here is the code that I used on my application update. You need to add this in your AppDelegate.m

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"YourDatabase.sqlite"];
    NSManagedObjectModel *managedObjectModel = [self managedObjectModel];
    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];

    // Check if we already have a persistent store
    if ( [[NSFileManager defaultManager] fileExistsAtPath: [storeURL path]] ) {
        NSDictionary *existingPersistentStoreMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType: NSSQLiteStoreType URL: storeURL error: &error];
        if ( !existingPersistentStoreMetadata ) {
            // Something *really* bad has happened to the persistent store
            //[NSException raise: NSInternalInconsistencyException format: @"Failed to read metadata for persistent store %@: %@", storeURL, error];
            NSLog(@"Failed to read metadata for persistent store %@: %@", storeURL, error);
        }

        if ( ![managedObjectModel isConfiguration: nil compatibleWithStoreMetadata: existingPersistentStoreMetadata] ) {
            if ( ![[NSFileManager defaultManager] removeItemAtURL: storeURL error: &error] )
                NSLog(@"*** Could not delete persistent store, %@", error);
        } // else the existing persistent store is compatible with the current model - nice!
    } // else no database file yet

    [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                              configuration:nil
                                                        URL:storeURL
                                                    options:nil
                                                      error:&error];
    return _persistentStoreCoordinator;
}

This code covers issues

  • if existing database is old one, than deleting and setting up new one.
  • if there is no database yet (user download only the newest version) than creating new database.
  • if existing database is compatible with new one, just using that.

Just change the @"YourDatabase.sqlite" to your sqliteDB filename and it will work fine.




回答2:


If you only added some entities/attributes you can use CoreData lightweight migrations:

1) Add a new version of the schema from the Editor menu with you xcdatamodeld file open.

2) Add the new entities attributes to this new schema version.

3) Set the new schema version as the active one in your xcdatamodeld options (left pane).

4) Set NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption to true in your persistentStore initialization method in the UIApplicationDelegate.

You should now be able to run the app. The schema should be updated automatically to match your new database structure. This will also preserve the database contents, emptying it is probably something you want to do in your code, detecting the first launch of the new version.



来源:https://stackoverflow.com/questions/19790558/coredata-after-application-update

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!