I have a confusion about how Dispatchers work in Kotlin
Task In my Application class I intend to access my database via Room, take out the user , take out his JWT accessToken and set it in another object that my retrofit Request inteceptor uses.
However I want all this code to be blocking , so that when the Application class has ran to its completion , the user has been extracted and set in the Inteceptor.
Problem My application class runs to completion BEFORE the user has been picked from the database.
Session class is the one which accesses Room
This is how my session class looks
class Session(private val userRepository: UserRepository, private var requestHeaders: RequestHeaders) { var authenticationState: AuthenticationState = AuthenticationState.UNAUTHENTICATED var loggedUser: User? by Delegates.observable<User?>(null) { _, _, user -> if (user != null) { user.run { loggedRoles = roleCsv.split(",") loggedRoles?.run { if (this[0] == "Employer") { employer = toEmployer() } else if (this[0] == "Employee") { employee = toEmployee() } } authenticationState = AuthenticationState.AUTHENTICATED requestHeaders.accessToken = accessToken } } else { loggedRoles = null employer = null employee = null authenticationState = AuthenticationState.UNAUTHENTICATED requestHeaders.accessToken = null } } var loggedRoles: List<String>? = null var employee: Employee? = null var employer: Employer? = null init { runBlocking(Dispatchers.IO) { loggedUser = userRepository.loggedInUser() Log.d("Session","User has been set") } } // var currentCity // var currentLanguage } enum class AuthenticationState { AUTHENTICATED, // Initial state, the user needs to secretQuestion UNAUTHENTICATED, // The user has authenticated successfully LOGGED_OUT, // The user has logged out. } This is my Application class
class MohreApplication : Application() { private val session:Session by inject() private val mohreDatabase:MohreDatabase by inject() // this is integral. Never remove this from here. This seeds the data on database creation override fun onCreate() { super.onCreate() startKoin { androidLogger() androidContext(this@MohreApplication) modules(listOf( platformModule, networkModule, .... )) } Log.d("Session","Launching application") } My Koin module which creates the session
val platformModule = module { // single { Navigator(androidApplication()) } single { Session(get(),get()) } single { CoroutineScope(Dispatchers.IO + Job()) } } In my Logcat first "Launching Application" prints out and THEN "User has been set"
Shouldn't it be reverse? . This is causing my application to launch without the Session having the user and my MainActivity complains.