如何在Core Data模型更改后从备份还原SQLite数据库(轻量级迁移) [英] How to Restore SQLite Database from Backup after Core Data model has changed (lightweight migration)

查看:200
本文介绍了如何在Core Data模型更改后从备份还原SQLite数据库(轻量级迁移)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据应用程序将其sqlite数据库备份到Dropbox,并且用户可以通过覆盖其当前数据库(如果/当他们需要时)来恢复它。

I have an core-data app that backs up its sqlite database to Dropbox, and the user can restore it by overwriting their current database if/when they need to.

在下一个应用程序版本中,核心数据模型已更改,现有用户的数据库将通过轻量级迁移过程自动更新。

In the next app release, the core-data model has changed, and the databases for existing users will be automatically updated through the lightweight migration process.

我担心的是与已备份的数据库。如果用户要恢复在迁移之前备份的sqlite数据库,它将不匹配最新的模型,它会使应用程序崩溃。

My concern is with the databases that have been backed up already. If a user goes to restore an sqlite database that was backed up before the migration, it will not match the latest model and it will crash the app.

有任何方式我可以在恢复过程中更新数据库,以匹配我的核心数据模型?

Is there any way that I can update a database during the restore process, to match my core-data model? Either a process that I can run, or some steps that I can take to make sure that the backup is not lost?

推荐答案

我可以运行一个进程,或者我可以采取一些步骤来确保备份不丢失?如果您使用核心数据打开sqlite数据库,轻量级迁移将自动进行。主要作为应用程序更新的一部分,但也打开已还原的数据库。

Lightweight migration does happen automatically if you open a sqlite database with core data. Mostly as part of an application update, but also when you open a restored database.

您可以在还原后添加检查以查看迁移是否必要:

You could add a check to see if migration is necessary after restore:

-(BOOL) storeRequiresMigration: (NSURL *) storeURL {
NSError *error = nil;
NSPersistentStoreCoordinator * temporaryPersistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

//Check if migration is needed
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:&error];
NSManagedObjectModel *destinationModel = [temporaryPersistentStoreCoordinator managedObjectModel];
BOOL isCompatibile = [destinationModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata];
NSLog(@"Store requires migration: %d", !isCompatibile);
return !isCompatibile;

}

这篇关于如何在Core Data模型更改后从备份还原SQLite数据库(轻量级迁移)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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