The Monster
01/15/2022, 2:53 PMscanAudios() and scanVideos() to execute first before other stuff in this function.
private suspend fun getFolderDataList(): List<FolderData> {
thisScope.async() {
scanAudios()
scanVideos()
}.await()
val audioFolderData = viewModel.getAudioUris().map { uri ->
FolderData("Audio", uri)
}
val videoFolderData = viewModel.getVideoUris().map { uri ->
FolderData("Video", uri)
}
return audioFolderData.plus(videoFolderData)
}
Am I doing it correctly?Dominaezzz
01/15/2022, 2:55 PMDominaezzz
01/15/2022, 2:56 PMawait after the async, but it should do what you want.Dominaezzz
01/15/2022, 2:57 PMasync there?The Monster
01/15/2022, 2:59 PMawait(). launch() does not have it. launch() only has invokeOnCompletion() which I cannot use because the other code are also suspend functions.Dominaezzz
01/15/2022, 3:01 PMlaunch has join .Dominaezzz
01/15/2022, 3:02 PMscanAudios and scanVideos without wrapping it in a coroutine?The Monster
01/15/2022, 3:03 PMjoin. Let me check it out.The Monster
01/15/2022, 3:04 PMscanAudios and scanVideos need to complete first for the code after to be accurate.Dominaezzz
01/15/2022, 3:05 PMDominaezzz
01/15/2022, 3:06 PMThe Monster
01/15/2022, 3:06 PMjoin or await then?Dominaezzz
01/15/2022, 3:07 PMprivate suspend fun getFolderDataList(): List<FolderData> {
scanAudios()
scanVideos()
val audioFolderData = viewModel.getAudioUris().map { uri ->
FolderData("Audio", uri)
}
val videoFolderData = viewModel.getVideoUris().map { uri ->
FolderData("Video", uri)
}
return audioFolderData.plus(videoFolderData)
}Dominaezzz
01/15/2022, 3:08 PMscanAudios do? It's a suspend function right?The Monster
01/15/2022, 3:09 PMgetAudioUris() and getVideoUris() won't be executed when `scanAudios`/`scanVideos` are running?The Monster
01/15/2022, 3:10 PMscanAudios is a suspend function. getAudioUris and getVideoUris are also suspend.Dominaezzz
01/15/2022, 3:12 PMlaunch or async.The Monster
01/15/2022, 3:12 PMscanAudios and scanVideos to finish first before calls to getAudioUris and getVideoUrisThe Monster
01/15/2022, 3:13 PMDominaezzz
01/15/2022, 3:14 PMThe Monster
01/15/2022, 3:14 PMThe Monster
01/15/2022, 3:14 PMDominaezzz
01/15/2022, 3:14 PMDominaezzz
01/15/2022, 3:15 PMprivate suspend fun getFolderDataList(): List<FolderData> {
coroutineScope {
launch { scanAudios() }
launch { scanVideos() }
}
val audioFolderData = viewModel.getAudioUris().map { uri ->
FolderData("Audio", uri)
}
val videoFolderData = viewModel.getVideoUris().map { uri ->
FolderData("Video", uri)
}
return audioFolderData.plus(videoFolderData)
}The Monster
01/15/2022, 3:16 PMJoffrey
01/15/2022, 3:32 PMasync{...}.await() is pointless and actually equivalent to just calling the functions. The only difference here is you're using a particular scope, so the coroutine context could be different, but in terms of what waits and what doesn't it's equivalentJoffrey
01/15/2022, 3:36 PMscanAudios and scanVideos concurrently, it's another story. But it doesn't seem to be your goal here, so you can keep things very simple