Ensure nested Coroutine is completed?

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) {
        INSTANCE?.let { database ->
            scope.launch {
                val puzzleDao = database.puzzleDao()

    private suspend fun prePopulateDatabase(puzzleDao: PuzzleDao) {
        val startingData = StartingData()


    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**

        // job is now complete
        loadPuzzlesIsCompleted = true


thank you

I don’t know Room, but is onCreate() called on the main thread? If so then you’d probably need to use runBlocking() to block that thread while your coroutine runs, which seems bad. If it runs on a background thread then you don’t need to use a coroutine.