不适当的阻塞方法调用,但应仅从协程或其他挂起函数调用挂起函数“withContext" [英] Inappropriate blocking method call, but Suspend function 'withContext' should be called only from a coroutine or another suspend function
问题描述
在我的服务中,我需要调用 onStartCommand
一些需要 withContext(Dispatchers.IO)
而不是 CoroutineScope(Dispatchers.IO)
的方法> 喜欢:
In my Service, I need to call on onStartCommand
some methods that require withContext(Dispatchers.IO)
instead CoroutineScope(Dispatchers.IO)
like:
- url = URL(pokemon.linkImage)
- url.openConnection().getInputStream()
- fOut= FileOutputStream(file)
- fOut.flush()
- fOut.close()
但是挂起函数'withContext'只能从协程或其他挂起函数调用
.因此,如果 onStartCommand
不能成为挂起函数,因为它具有覆盖,并且 withContext
不能被 CoroutineScope 调用,因为 不适当的阻塞方法调用
方法
But Suspend function 'withContext' should be called only from a coroutine or another suspend function
. So if onStartCommand
can't be a suspend function because it has override and withContext
can't be called by CoroutineScope because Inappropriate blocking method call
of methods
val url = URL(pokemon.linkImage)
val iS = url.openConnection().getInputStream()
我该如何解决这个问题?
How can I resolve this problem?
我的onStartCommand():
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
//create the directory on internal storage that will contains all pokemon images
val path = applicationContext.filesDir.absolutePath
val dirName = "/pokeimg"
val dir = File(path, dirName)
if(!dir.exists())
dir.mkdir()
CoroutineScope(Dispatchers.IO).launch {
//get the list of pokemon when they are loaded from repository
val listOfPokemon =
PersistenceSingletonRepository.getInstance(applicationContext).getListOfPokemon()
//download all the image for each pokemon in the list, but only if the image is
//not present in the directory
for(pokemon in listOfPokemon) {
val imageName = "${pokemon.name}.png"
val file = File("${dir.absolutePath}/$imageName")
//check if image not exists on internal storage and if is not an official-artwork
//images that aren't official-artwork has less quality so don't resize
if(!file.exists()) {
//download img
val url = URL(pokemon.linkImage)
val iS = url.openConnection().getInputStream()
val opts = BitmapFactory.Options()
if (!Utils.isBadImage(pokemon.linkImage)) {
//request a smaller image
opts.inSampleSize = 2
}
val bitmap = BitmapFactory.decodeStream(iS, null, opts)
val fOut= FileOutputStream(file)
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, fOut)
fOut.flush()
fOut.close()
}
}
stopSelf()
}
return START_NOT_STICKY
}
推荐答案
您可以放心地忽略该警告,众所周知,它有许多误报.您在 IO 调度器中启动了协程,该调度器专为阻塞调用而设计.
You can safely ignore that warning, it's known to have many false positives. You launched the coroutine in the IO dispatcher, which is designed for blocking calls.
另一方面,使用没有父级和生命周期绑定的临时 CoroutineScope
启动任何东西通常是错误的做法.您应该尊重 Service 生命周期并在 onDestroy
中取消您的协程.例如,请参见此处.
On the other hand, launching anything with an ad-hoc CoroutineScope
that has no parent and no lifecycle binding, is usually the wrong thing to do. You should respect the Service lifecycle and cancel your coroutine in onDestroy
. See for example here.
这篇关于不适当的阻塞方法调用,但应仅从协程或其他挂起函数调用挂起函数“withContext"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!