2

首先,我不是 100% 了解 Core Data,但我尽我所能。所以我在应用程序更新时实现了轻量级迁移,但最近失败了,即应用程序在尝试访问本地数据库后崩溃。我目前认为原因是由于模型版本有些混淆,但即使不是这样,我认为我的问题仍然有效:

有没有办法在更新/升级应用程序时忽略核心数据迁移过程并强制应用程序使用最新的模型版本,即使它删除了本地用户数据?

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

4

1 回答 1

2

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

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

这在开发过程中也很有用,因为您不必在每次更改模型时都删除应用程序。

于 2013-11-11T08:07:20.080 回答