Hi guys!
Firstly, I’m sorry this bad English I usually don’t speak or write English.
After my databaseBuilder(…).build(), the callback method [onCreated] isn’t called
I tried to put this line after build()…
instance.openHelper.writableDatabase
…the onCreated method is called!
But when I build on the cellphone the app is crashed, asking for MIGRATION_1_2.
I want an explanation, please
I need the callback methods, but the DATABASE isn’t created calling only build(), why?
Edit:
I’ve already tried to reinstall the application too.
Follow my AppRoomDatabase
@Database(entities = [User::class, Setting::class], version = 1)
abstract class AppRoomDatabase : RoomDatabase() {
abstract fun userDAO(): UserDAO
abstract fun settingDAO(): SettingDAO
private class AppRoomCallback(private val scope: CoroutineScope) :
RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
INSTANCE?.let { database ->
scope.launch {
initialize(database)
}
}
}
suspend fun initialize(database: AppRoomDatabase) {
val userDAO = database.userDAO()
userDAO.insert(
User(
"demo",
"demo",
"Usuário Demonstração",
"demo@testapp.com",
1
)
)
val settingDAO = database.settingDAO()
settingDAO.insert(
Setting(
"DEFAULT",
null,
null,
true,
IpAddress("10.1.1.1", "8080"),
Period(null, null)
)
)
}
}
companion object {
@Volatile
private var INSTANCE: AppRoomDatabase? = null
fun getDatabase(context: Context, scope: CoroutineScope): AppRoomDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppRoomDatabase::class.java,
"app_database")
.addCallback(AppRoomCallback(scope))
.build()
INSTANCE = instance
return@synchronized instance
}
}
}
}