【发布时间】:2019-06-19 13:03:07
【问题描述】:
我是 kotlin 协程的新手,有一些疑问。因此,我尝试使用 kotlin 协程下载字体列表,并添加了一些日志以查看何时下载字体,或者在字体已存在时显示消息。我希望每次访问字体时都会看到一个日志,但是我只看到进度条,当它被隐藏时,我会立即看到所有日志。我做错了吗?
private fun init() {
val job = Job()
val bgScope = CoroutineScope(Dispatchers.IO + job)
bgScope.launch {
getStuff()
}
}
fun getStuff() {
val uiScope = CoroutineScope(Dispatchers.Main + Job())
uiScope.launch {
progressbar.visibility = View.VISIBLE
}
for (font in jsonObject.fontList) {
if (!font.exists()) {
downloadFile(font)
Timber.d("file " + font.id + " downloaded: " + font.exists())
} else {
Timber.d("file " + font.id + " already exists ")
}
}
uiScope.launch {
progressbar.visibility = View.GONE
}
【问题讨论】:
-
不确定这是否与您的问题有关,但
launch启动了一个可能与下载并行运行的作业。您可能想改用withContext,这样您就可以确保在开始下载时进度条是可见的。
标签: android kotlin kotlin-coroutines