https://kotlinlang.org logo
j

Justin Yue

06/24/2021, 6:12 AM
I'm looking to wait for a coroutine to finish before continuing with some other processes. From what I've found in Kotlin and Android documentation,
async
would be my best bet since it returns a
Deferred
that "represents a promise to provide a result later." However, I am not looking to wait for a result to be return. While I know I could probably return a dummy value (probably a boolean to represent the coroutine finished), are there any other alternatives to
async
?
l

louiscad

06/24/2021, 6:26 AM
Just make the function suspend.
j

Justin Yue

06/24/2021, 6:37 AM
Code:
suspend fun extractTextFromFilePath(filePath: String): Boolean {
// code
}
class CameraViewModel(val context: Context): ViewModel() {
private var textBlocks = mutableMapOf<String, Array<Point?>>()
val textBlockString
get() = textBlocks.toString()
fun extractText(filePath: String): String {
viewModelScope.launch {
imageAnalyzer.extractTextFromFilePath(filePath)
}
Log.d(TAG, textBlockString)
return extractedText
}
}
I changed my extractTextFromFilePath() function to a suspend function and ran it in the viewModelScope, but when I check my logs, textBlockString is an empty string. If I were to run it a little while later, I can see that the
textBlocksString
is no longer an empty string
I plan to process
textBlocksString
, so how can I ensure that I run the other processing when `textBlocksString`has been properly updated by
extractTextFromFilePath()
4 Views