'Room databse loses data on restart application
According to documentation room instance from Room.databaseBuilder() should save data is persist. But still get lost. My Project have to database
- First Database
@Database(entities = [FoodModel::class], version = 4, exportSchema = false) abstract class FoodDatabase : RoomDatabase() {
abstract val foodDatabaseDao: FoodDatabaseDao
companion object {
@Volatile
private var INSTANCE: FoodDatabase? = null
fun getInstance(context: Context): FoodDatabase {
synchronized(this) {
var instance = INSTANCE
if (instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
FoodDatabase::class.java,
Constants.OVERVIEW_FOOD_DATABASE
)
.fallbackToDestructiveMigration()
.build()
INSTANCE = instance
}
return instance
}
}
}
}
Second Databse
@Database(entities = [MyFoodModel::class], version = 3, exportSchema = false)abstract class MyFoodDatabase : RoomDatabase() { abstract val myFoodDatabaseDao: MyFoodDatabaseDao
companion object { @Volatile private var INSTANCE: MyFoodDatabase? = null fun getInstance(context: Context): MyFoodDatabase { synchronized(this) { var instance = INSTANCE if (instance == null) { instance = Room.databaseBuilder( context.applicationContext, MyFoodDatabase::class.java, Constants.OVERVIEW_FOOD_DATABASE ) .fallbackToDestructiveMigration() .build() INSTANCE = instance } return instance } } }}
Dao of first Database
@Dao
interface MyFoodDatabaseDao {
@Insert
fun insert(food: MyFoodModel)
@Query("SELECT * FROM MyFoodItems ORDER BY name DESC")
fun getAllFood(): LiveData<List<MyFoodModel>>
@Delete
fun deleteFood(foodModel: MyFoodModel)
}
Dao of Second database
@Dao
interface MyFoodDatabaseDao {
@Insert
fun insert(food: MyFoodModel)
@Query("SELECT * FROM MyFoodItems ORDER BY name DESC")
fun getAllFood(): LiveData<List<MyFoodModel>>
@Delete
fun deleteFood(foodModel: MyFoodModel)
}
Solution 1:[1]
An android application can have more than one database.
Here as I can see, You are providing same name [Constants.OVERVIEW_FOOD_DATABASE] to your both the databases [MyFoodDatabase, FoodDatabase]. So all values will be written in one database named as Constants.OVERVIEW_FOOD_DATABASE.
Please provide both the database different name and try again.
Edited
As you said, you are using two different instance of same databases and for every database instance, you are changing the database version but you are not migrating your database into that version. Instead you are using fallbackToDestructiveMigration() that does not crash database but clear the data when any existing version is found.
Please try below steps:
- remove fallbackToDestructiveMigration() from both database instances.
- in second instance add .addMigrations(MIGRATION_1_2) while creating instance
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
// do nothing because you are not altering any table
}
}
- in First instance add .addMigrations(MIGRATION_2_1) while creating instance
val MIGRATION_2_1 = object : Migration(2, 1) {
override fun migrate(database: SupportSQLiteDatabase) {
// do nothing because you are not altering any table
}
}
It will migrate you same database. In my case it is working. I hope it will work in your case too. :)
But it is better to use single database instance and include the list of entities associated with the database within the annotation. Because room database instances are expensive.
https://developer.android.com/training/data-storage/room
Note: If your app runs in a single process, you should follow the singleton design pattern when instantiating an AppDatabase object. Each RoomDatabase instance is fairly expensive, and you rarely need access to multiple instances within a single process.
If your app runs in multiple processes, include enableMultiInstanceInvalidation() in your database builder invocation. That way, when you have an instance of AppDatabase in each process, you can invalidate the shared database file in one process, and this invalidation automatically propagates to the instances of AppDatabase within other processes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ruben Helsloot |
