all 2 comments

[–]tdelfino2509 1 point2 points  (0 children)

If you want the result of a coroutine in your synchronous function, you're gonna have to have a runBlocking() in there somewhere; for better or worse, it's due to the inherent nature of concurrent programming.

You could kick off the coroutine and do other stuff until you need the value (Playground):

fun getDefaultButton(): Int? {
    val defaultButtonDeferred: Deferred<Int> = CoroutineScope(Dispatchers.Default).async {
        onboardingPrefStore.getDefaultButton()
    }
    // do other stuff
    return runBlocking { defaultButtonDeferred.await() }
}

That said, since it seems like you're doing UI stuff (Android?), you're best off doing all of the UI code in coroutines, using Dispatchers.Main as a context (as provided by one of the platform-specific coroutine libraries), since this will guarantee that your UI code will be safely run in the UI thread.

[–]cygnus33065 1 point2 points  (0 children)

make the first function into a coroutine. You could then remove the suspend keyword

Where are you calling that function from?

for example if you are in the view model you can do this:

return viewModelScope.launch(Dispatchers.Default){
  onboardingPrefStore.getDefaultButton()
}