Realm 2.1.1 + kotlin 1.1.3 environment.
The exception "RealmMigrationNeedException: Field count is more than expected" always occurs during maigration.
Before migration
public open class TestObject():RealmObject(){
@PrimaryKey
open var name: String=""
open range: Int=0
}
After migration
public open class TestObject():RealmObject(){
@PrimaryKey
open var name: String=""
open range: Int=0
open var hogehoge: String=""// Add
}
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){
vara = schema.get ("TestObject")
a.addField("hogehoge", String::class.java, FieldAttribute.REQUIRED)
oldVersion++
}
}
}
application maigration portion
Realm.init(this)
val builder=RealmConfiguration.Builder()
builder.schemaVersion(1L).migration(MyMigration())
val config = builder.build()
Realm.setDefaultConfiguration(config)
Where the error occurred
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()// Error Occurred
Error Contents
Cause by:io.realm.exception.RealmMigrationNeededException: Field count is more than expected-expected2 but was 3
atio.realm.TestObjectRealmProxy.validateTable (TestObjectRealmProxy.java:182)
atio.realm.DefaultRealmModuleMediator.validateTable (DefaultRealmModuleMediator.java:186)
atio.realm.Realm.initializeRealm(Realm.java:344)
atio.realm.Realm.createAndValidate (Realm.java:301)
atio.realm.Realm.createInstance(Realm.java:280)
atio.realm.RealmCache.createRealmOrGetFromCache (RealmCache.java:143)
atio.realm.Realm.getDefaultInstance(Realm.java:211)
at jp.co.ftaba_d.bluebear.MainActivity.onCreate(MainActivity.kt:103)
It always happens regardless of object type.
I would appreciate it if someone could teach me.
The defined model does not have @Required
annotation, so FieldAttribute.REQUIRED
is not required to add fields in the migration class. github.com/realm/realm-java/issues/4701 is considering automatically @Required
if the model is non-nullable.
If you feel it is not reflected after modifying the model class, try cleaning the project.
Disabling incremental builds by specifying kotlin.incremental=false
in build.gradle
may also work.
© 2024 OneMinuteCode. All rights reserved.