androidkotlinrealmrealm-migration

how can i Migrate Realm without losing any data - Kotlin


I've been trying to rename field or add new field to my realm database. But i'm getting error like this. I tried to debug my app it looks like oldVersion++ is not increasing the old schema version. Detailed Error is down below

Any help would be appreciated.

io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
Property 'TransferDetailModel.MainUniqueId' has been made optional.

Migration.kt

open class Migration : RealmMigration {

    override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {

        var oldVersion = oldVersion
        val schema = realm.schema
 if (oldVersion == 0L) {
            val tdmSchema = schema.get("TransferDetailModel")

            tdmSchema!!
                    .addField("MainUniqueId", String::class.java, FieldAttribute.REQUIRED)
                    .transform { obj -> obj.set("MainUniqueId", obj.getString("DetailUniqueId"))}
                    .removeField("DetailUniqueId")
            oldVersion++
        }
    }
}
`

BaseApplication.kt

open class BaseApplication: Application() {

    override fun onCreate() {
        super.onCreate()
        Realm.init(this)
        var mConfiguration = RealmConfiguration.Builder()
                .name("BSOMSDB.realm")
                .schemaVersion(1)
                .migration(Migration())
                .build()
        Realm.setDefaultConfiguration(mConfiguration)

        val realm = Realm.getDefaultInstance()
    }
}

ProductTransferMain.kt

lateinit var realm: Realm

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_product_transfer_main)
        Realm.init(this)

realm = Realm.getDefaultInstance() // Getting error at this line

TransferDetailModel

@RealmClass
open class TransferDetailModel: RealmObject() {

    @PrimaryKey
    private var DetailId:Long = 0
    private var MainId:Long = 0
    private var MainUniqueId: String = ""
    private var Barcode:String = ""
    private var Quantity:Int = 0
    private lateinit var InsertDate: Date

    fun setMainUniqueId(MainUniqueId:String)
    {
        this.MainUniqueId=MainUniqueId
    }
    fun setDetailId(DetailId:Long)
    {
        this.DetailId=DetailId
    }
    fun setMainId(MainId:Long)
    {
        this.MainId=MainId
    }
    fun setBarcode(Barcode:String)
    {
        this.Barcode=Barcode
    }
    fun setQuantity(Quantity:Int)
    {
        this.Quantity=Quantity
    }
    fun setInsertDate(InsertDate:Date)
    {
        this.InsertDate=InsertDate
    }

    fun getDetailId():Long
    {
        return DetailId
    }
    fun getMainId():Long
    {
        return MainId
    }
    fun getMainUniqueId():String
    {
        return MainUniqueId
    }
    fun getBarcode():String
    {
        return Barcode
    }

    fun getQuantity():Int
    {
        return Quantity
    }
    fun getInsertDate():Date
    {
        return InsertDate
    }
}

Error Output 

09-09 23:17:49.687 32090-32090/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.bambiEnt.BambiSOMS, PID: 32090
    java.lang.RuntimeException: Unable to create application com.bambiEnt.BambiSOMS.classes.BaseApplication: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
    - Property 'TransferDetailModel.MainUniqueId' has been made optional.
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4966)
        at android.app.ActivityThread.-wrap1(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1553)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5737)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
     Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
    - Property 'TransferDetailModel.MainUniqueId' has been made optional.
        at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
        at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:171)
        at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:241)
        at io.realm.BaseRealm.<init>(BaseRealm.java:136)
        at io.realm.BaseRealm.<init>(BaseRealm.java:103)
        at io.realm.Realm.<init>(Realm.java:163)
        at io.realm.Realm.createInstance(Realm.java:499)
        at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:355)
        at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:285)
        at io.realm.Realm.getDefaultInstance(Realm.java:407)
        at com.bambiEnt.BambiSOMS.classes.BaseApplication.onCreate(BaseApplication.kt:21)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1018)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4963)
            ... 8 more

Solution

  • Realm.init(this) sets a default RealmConfiguration and should ONLY be called inside Application.onCreate.

    The default configuration overrides your RealmConfiguration, and that's why the migration doesn't run.