Realm 2.1.1 + kotlin 1.1.3 の環境です。
maigrationの過程で必ず「RealmMigrationNeededException: Field count is more than expected」の例外が発生します。
migration前
public open class TestObject():RealmObject(){
@PrimaryKey
open var name:String = ""
open var age:Int = 0
}
migration後
public open class TestObject():RealmObject(){
@PrimaryKey
open var name:String = ""
open var age:Int = 0
open var hogehoge:String = ""//追加
}
migrationObject
class MyMigration:RealmMigration{
override fun migrate(realm: DynamicRealm?, oldVersion: Long, newVersion: Long) {
Log.i("","oldVersion $oldVersion newVersion $newVersion")
var oldVersion = oldVersion
val schema = realm!!.schema
if(oldVersion == 0L){
var a = schema.get("TestObject")
a.addField("hogehoge", String::class.java, FieldAttribute.REQUIRED)
oldVersion++
}
}
}
appllicationのmaigration部分
Realm.init(this)
val builder = RealmConfiguration.Builder()
builder.schemaVersion(1L).migration(MyMigration())
val config = builder.build()
Realm.setDefaultConfiguration(config)
error発生箇所
MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Fabric.with(this, Crashlytics(), CrashlyticsNdk());
setContentView(R.layout.activity_start)
Log.i("mainActivity","startMainActivity")
val realm = Realm.getDefaultInstance()//エラー発生
エラー内容
Caused by: io.realm.exceptions.RealmMigrationNeededException: Field count is more than expected - expected 2 but was 3
at io.realm.TestObjectRealmProxy.validateTable(TestObjectRealmProxy.java:182)
at io.realm.DefaultRealmModuleMediator.validateTable(DefaultRealmModuleMediator.java:186)
at io.realm.Realm.initializeRealm(Realm.java:344)
at io.realm.Realm.createAndValidate(Realm.java:301)
at io.realm.Realm.createInstance(Realm.java:280)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:143)
at io.realm.Realm.getDefaultInstance(Realm.java:211)
at jp.co.futaba_d.bluebear.MainActivity.onCreate(MainActivity.kt:103)
オブジェクトの種類を問わず必ず発生します。
どなたか、ご教授願えれば幸いです。