为什么我的Kotlin函数从firebase存储加载图像这么慢?



我对编程很陌生,对于这个应用程序,我在Android Studio中使用Kotlin。我有一个函数,从firebase存储加载存储的图像,这两个图像都可供所有用户使用,也是"私人"图像。它工作得很好,但需要大约20-30秒来加载图像。我想应该能修复得更快吧?

函数现在的样子:

private fun listStoredImages() = CoroutineScope(Dispatchers.IO).launch {
try {
val userImages = imageRef.child(uid).listAll().await()
val publicImages = imageRef.child("UploadedPictures").listAll().await()
// Loads private images from storage
for (image in userImages.items) {
val url = image.downloadUrl.await()
userImageUrl.add(url.toString())
}   // loads public images from storage
for (publicImage in publicImages.items) {
val publicUrl = publicImage.downloadUrl.await()
userImageUrl.add(publicUrl.toString())
}
withContext(Dispatchers.Main) {
recyclerView.adapter?.notifyDataSetChanged()
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Toast.makeText(this@UserCreateAndEditActivity, "Error", Toast.LENGTH_SHORT).show()
}
}
}

imageRef:

val imageRef = Firebase.storage.reference

我不知道它是否有任何不同,但我正在使用网格来显示图像。这样的:

private var gridLayoutManager: GridLayoutManager? = null

在创建:

gridLayoutManager =
GridLayoutManager(applicationContext, 3, LinearLayoutManager.VERTICAL, false)
recyclerView.setHasFixedSize(true)
imageAdapter = ImageAdapter(this, userImageUrl)
recyclerView.layoutManager = gridLayoutManager
recyclerView.adapter = imageAdapter

有什么办法让它更快吗?

问题是我只是用

更新我的视图
withContext(Dispatchers.Main) {
recyclerView.adapter?.notifyDataSetChanged()
}

在所有图片url完成加载后。当我在每个图像加载后的for循环中添加了notifyDataSetChanged后,它就快多了。

最新更新