是否可以“强制”应用程序接受一个新的模型版本,即使它意味着覆盖现有的吗? [英] Is it possible to 'force' the app to accept a new model version even if it means overwriting the existing one?

查看:79
本文介绍了是否可以“强制”应用程序接受一个新的模型版本,即使它意味着覆盖现有的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不是100%的核心数据,但我尽我所能。所以我实现了轻量级迁移,当应用程序更新,但最近这失败,即应用程序崩溃后试图访问本地数据库。我目前假设的原因是因为一些混合的模型版本,但即使不是这样,我想我的问题仍然有效:

Firstly, I'm not 100% clued up on Core Data, but I do what I can. So I've implemented Lightweight Migration for when the app is updated, but recently this has failed, i.e. the app crashes after trying to access the local DB. I currently assume that the reason is because of some mix up with the model versions, but even if not so, I think my question is still valid:

有一种方式,在更新/升级应用程序时,忽略Core Data迁移过程,并强制应用程序使用最新的模型版本,即使它删除本地用户数据?

Is there a way, when updating/upgrading an app, to ignore the Core Data migration process and force the app to use the latest model version, even if it deletes the local user data?

我的计划是,如果迁移失败,强制最新版本到设备上。这是一个比崩溃应用程序更好的解决方案

My plan is that if the migration fails, force the latest version onto the device. That's a better solution than a crashing app

推荐答案

迁移发生在 addPersistentStoreWithType 调用。因此,如果
失败,并且你想从一个新的空数据库开始,只需删除持久存储
文件并再次调用 addPersistentStoreWithType

The migration happens during the addPersistentStoreWithType call. So if that fails and you want to start with a new empty database, just remove the persistent store file and call addPersistentStoreWithType again:

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:NULL];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        // Handle fatal error
    }
}

时也很有用,因为你每次更改模型时都不必删除应用

This is also useful during development, because you don't have to delete the app each time you change your model.

这篇关于是否可以“强制”应用程序接受一个新的模型版本,即使它意味着覆盖现有的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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