Home > Back-end >  Room Insert Not Persisting between Runs in Kotlin Jetpack Compose Project
Room Insert Not Persisting between Runs in Kotlin Jetpack Compose Project

Time:04-18

I'm working on an Android Jetpack Compose project with pre-populated Room Database.

Reading the database works great, however...

When I INSERT a new record, it doesn't PERSIST into the next time I run the application. I'm NOT using inMemoryDatabaseBuilder().

I know it works while the application is in memory because I can see the results on the screen, however, as soon as I stop the application, my insert goes away. I also checked the underlying database file and my insert is never committed to the database either while it's running or after I close it.

What am I doing wrong?

Thank you for your help!


This is the statement in my User Interface that adds a new person to my database:

viewModel.insertMyInfo(newMyInfo)

It works in memory, but the newly added record disappears as soon as I exit the application.

App Inspector LiveData shows my additions.


viewModel Insert Function

fun insertMyInfo(myInfo: MyInfo) {
    myInfoRepository.insertMyInfo(myInfo)
}

Only seems to commit to memory, not disk.


DAO @Insert

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertMyInfo(vararg myInfo: MyInfo)

I tried this with suspend and no difference


Repository Function

fun insertMyInfo(newMyInfo: MyInfo) {
    coroutineScope.launch(Dispatchers.IO) {
        myInfoDAO.insertMyInfo(newMyInfo)
    }
}

Database

@Database(entities = [(MyInfo::class)], version = 5)
abstract class MyInfoRoomDatabase: RoomDatabase() {

    abstract fun myInfoDao(): MyInfoDAO
    companion object {

        private var INSTANCE: MyInfoRoomDatabase? = null

        fun getInstance(context: Context): MyInfoRoomDatabase {
            synchronized(this) {
                var instance = INSTANCE

                if (instance == null) {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        MyInfoRoomDatabase::class.java,
                        "MyInfo"
                    ).createFromAsset("MyInfo.db")
                        .fallbackToDestructiveMigration()
                        .build()   // End Room Database Builder

                    INSTANCE = instance
                }   // End If Null

                return instance

            }   // End Synchronized

        }   // End Get Instance

    }   // End Companion Object

}   // End Database Abstract Class

CodePudding user response:

Your issue is probably that rather than the inserts not being applied, which you see they are, is that your issue is due to the version number being increased, along with .fallbackToDestructiveMigration in conjunction with no Migration being found.

This results in the database being destroyed, then as the database doesn't exist that it is created from the asset file and thus the changes are effectively undone.

As such, if there is a schema change (a change made to an @Entity annotated class that is included in the list of entities defined as part of the @Database annotation), then a Migration should be provided that makes the changes to the existing database, that retains the data, for the version change (in your case that handles the schema change from version 4 to version 5). An alternative could be to utilise AutoMigrations (noting the restrictions and requirements) see https://medium.com/androiddevelopers/room-auto-migrations-d5370b0ca6eb

  • Related