应用程序更新后的Coredata [英] Coredata after Application Update

查看:83
本文介绍了应用程序更新后的Coredata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用程序实现一个流程,该流程可以在需要时更新该应用程序,而通过更新,我意味着将新的应用程序.ipa加载到设备中。

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.

问题是我正在使用CoreData存储首次启动时带来的服务器数据,并且在旧版本和新版本之间添加了一些实体并将某些旧实体归入数据库。这引起了冲突,因为我不知道如何处理迁移和/或任何可以使我能够在结构更改时重新创建数据库的事物。

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.

任何想法?

推荐答案

如果可以从服务器中重新创建/下载应用程序中的数据,那将是一个很好的解决方案。
据我了解,您正在从服务器获取数据,这是一个很好的例子,这意味着可以在新数据库中重新创建旧数据。
您不需要设置迁移堆栈,这是一个非常快速的解决方案。诀窍是删除旧的sqlite数据库并创建一个新的sqlite数据库。

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.

这是我在应用程序更新中使用的代码。
您需要在AppDelegate.m中添加它。m

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;
}

此代码涵盖了问题


  • 如果现有数据库是旧数据库,则删除并设置新数据库。

  • 如果尚无数据库(用户仅下载最新版本),则比创建新数据库数据库。

  • 如果现有数据库与新数据库兼容,只需使用。

将@ YourDatabase.sqlite更改为sqliteDB文件名,它将正常工作。

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

这篇关于应用程序更新后的Coredata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆