如何避免在RealmSwift中迁移 [英] How can I avoid migration in RealmSwift

查看:70
本文介绍了如何避免在RealmSwift中迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在使用Realm测试某些配置,因此我在我的领域类中添加和删除了变量和列表.由于我只是测试,所以我不想经历迁移过程-我也没有任何影响连续性的数据.

I am just testing some configurations with Realm and therefore I have added and deleted variables and lists from my realm classes. Since I am just testing I do not want to go through the migration process - I also don't have any data which is effected for continuity.

Realm是否有任何解决迁移请求的方法?

Is there any way to get around migration being requested automatically by Realm?

推荐答案

有两种方法可以跳过迁移错误,无论架构如何更改.

There are two ways to skip migration error regardless schema changes.

  1. 使用deleteRealmIfMigrationNeeded属性.如果为true,则需要迁移时,使用提供的架构重新创建Realm文件.

  1. Use deleteRealmIfMigrationNeeded property. If it is true, recreate the Realm file with the provided schema if a migration is required.

let config = Realm.Configuration(deleteRealmIfMigrationNeeded: true)
Realm.Configuration.defaultConfiguration = config

let realm = try! Realm()
...

每次启动时递增架构版本. Realm具有自动迁移功能.如果不需要迁移现有数据,则只需增加架构版本即可.模式将由Realm自动更改.

Increment schema version every launch. Realm has auto migration feature. If you don't need to migrate existing data, you can just increment schema version. Schema will be changed by Realm automatically.

let config = Realm.Configuration(schemaVersion: try! schemaVersionAtURL(Realm.Configuration.defaultConfiguration.fileURL!) + 1)
Realm.Configuration.defaultConfiguration = config

let realm = try! Realm()
...

这篇关于如何避免在RealmSwift中迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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