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 PMawait
after the async
, but it should do what you want.async
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
.scanAudios
and scanVideos
without wrapping it in a coroutine?The Monster
01/15/2022, 3:03 PMjoin
. Let me check it out.scanAudios
and scanVideos
need to complete first for the code after to be accurate.Dominaezzz
01/15/2022, 3:05 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)
}
scanAudios
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?scanAudios
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 getVideoUris
Dominaezzz
01/15/2022, 3:14 PMThe Monster
01/15/2022, 3:14 PMDominaezzz
01/15/2022, 3:14 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 equivalentscanAudios
and scanVideos
concurrently, it's another story. But it doesn't seem to be your goal here, so you can keep things very simple