Hi! Just wondering the best way to go about solving this issue; I am using RoomDatabase.Callback to override and call OnCreate to pre-fill my Room database with data. I can’t work out how to ensure the data is inserted (during OnCreate) before moving on.
Code is as follows;
private class AppDatabaseCallback(private val scope: CoroutineScope) : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
INSTANCE?.let { database ->
scope.launch {
val puzzleDao = database.puzzleDao()
prePopulateDatabase(puzzleDao)
}
}
}
private suspend fun prePopulateDatabase(puzzleDao: PuzzleDao) {
val startingData = StartingData()
puzzleDao.insertAllPuzzles(startingData.getStartingPuzzles())
}
}
and
loadPuzzlesJob = CoroutineScope(Dispatchers.IO).launch {
// this either returns the existing roomdb, or creates it and fills it with the pre-data
val puzzleDao = AppDatabase.getDatabase(requireContext(), CoroutineScope(Dispatchers.IO)).puzzleDao()
// data should now be available
// PROBLEM - this is running before prePopulateDatabase is, so no data is returned
allPuzzles = puzzleDao.getAllPuzzles().toMutableList()
if (allPuzzles.isEmpty()) {
// puzzles not found, send to error screen
**// ALWAYS EMPTY, as prePopulateDatabase hasn't been called yet**
errorFound(ErrorType.PuzzlesNotFound)
}
// job is now complete
loadPuzzlesIsCompleted = true
progressBarUpdate()
loadPuzzlesJob?.cancelAndJoin()
}
thank you