0

I am prepopulating apps database with hilt databases onCreate method and everything is working good so far. I have a problem where my app does not persist updated data in database. I believe prepopulating database is happing and resetting the data or something else that I dont know of. The data stays updated as long as the app is running or in background, removing from background and starting again resets all things, much like this question.

I need a way to persist edited or updated data in my app and populate data only once after app installation.

table creation statement to prepopulate data:

CREATE TABLE words (id INTEGER PRIMARY KEY, en TEXT, pl TEXT, speech TEXT)

Entity:

@Entity(tableName = "words")
data class MyEntity (
    @PrimaryKey
    @ColumnInfo(name = "id")val id: Int,
    @ColumnInfo(name = "en") val englishWord: String?,
    @ColumnInfo(name = "pl") val polishWord: String?,
    @ColumnInfo(name = "speech") val speech: String?
)

hilt provider:

@Provides
@Singleton
fun provide(@ApplicationContext context: Context) = Room.databaseBuilder(
    context, AppDatabase::class.java, MY_DATABASE)
    .createFromAsset("database/database.db")
    //.allowMainThreadQueries()
    .fallbackToDestructiveMigration()
    .build()

DAO:

@Query("SELECT * FROM words")
fun getAll(): LiveData<List<MyEntity>>

@Update
fun updateThat(ent: MyEntity)

Here, after using update, it shows update at that moment and as long as the app stays in background but after restart, the update is gone.

Update: I have found solution from here.

Rifat
  • 1,700
  • 3
  • 20
  • 51

1 Answers1

0

Is the database schema version in your app different to the version of your asset database/database.db?

You have enabled fallbackToDestructiveMigration which would recreate the database each time the schema changes, as defined in the docs:

https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.Builder#fallbacktodestructivemigration

JakeB
  • 2,043
  • 3
  • 12
  • 19