如果需要迁移,领域不会自动删除数据库 [英] Realm not auto-deleting database if migration needed
问题描述
我们正在开发中,数据库模式更改经常发生.由于我们还没有生活,所以不需要迁移.为此,我对Realm进行了如下配置:
We are in development and db schema changes occur often. Since we are not live, migrations are not needed. I therefor configured Realm as follows:
RealmConfiguration config = new RealmConfiguration.Builder(context)
.name("jt.realm")
.schemaVersion(1)
.deleteRealmIfMigrationNeeded() // todo remove for production
.build();
Realm.setDefaultConfiguration(config);
但是,更改架构时会引发异常:RealmMigration must be provided
However, when the schema is changed, an exception is thrown: RealmMigration must be provided
我从文档中了解到,由于deleteRealmIfMigrationNeeded()存在于配置中,因此Realm应该自动删除数据库,但这似乎没有发生.为什么会这样?
My understanding from the docs are that the Realm should auto-delete the db since deleteRealmIfMigrationNeeded() is present in the config, but this does not seem to be happening. Why is this occurring?
Android Studio依赖项
编译'io.realm:realm-android:0.86.1'
compile 'io.realm:realm-android:0.86.1'
推荐答案
我们遇到了类似的问题.我们通过添加
We had a similar issue. We solved this by adding
Realm.getInstance(config)
紧接着
Realm.setDefaultConfiguration(config);
我们认为该配置将在首次调用Realm之后进行设置.这次我们不使用任何Realm对象,因此也不例外.
We think the configuration will be set up after Realm is called the first time. This time we don't use any Realm object so there's no exception.
这篇关于如果需要迁移,领域不会自动删除数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!