使用协程是一种选择。
创建您的挂起函数:
suspend fun getImages() : ArrayList<VideoData> {
withContext(Dispatchers.IO) {
// Dispatchers.IO
/* perform blocking network IO here */
}
}
suspend fun getVideos() : ArrayList<ImageData> {...}
suspend fun getAudio() : ArrayList<AudioData> {...}
创建工作
val coroutineJob_1 = Job()
创建范围
val coroutineScope_1 = CoroutineScope(coroutineJob + Dispatchers.Main)
在您的活动/片段中启动具有范围的作业...
coroutineScope_1.launch {
// Await
val response = getImages()
show(response)
}
show()有你的用户界面代码。
您可以启动多个作业以并行工作...
coroutineScope_2.launch {...}
coroutineScope_3.launch {...}