Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
Inappropriate blocking method call output stream write

I am using BufferedOutputStream

suspend fun write(byteArray: ByteArray) {    
        bos.write(byteArray)
}

But when I add suspend keyword I got warning:

Inappropriate blocking method call

What is the correct way to use output stream with coroutines?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In general if there is a truly async alternative, it better suits the coroutines model. You could find callback-based APIs and wrap them into suspend functions using suspendCoroutine or suspendCancellableCoroutine.

However, more often than not, you need to deal with actual blocking IO.

In that case, the easiest is to simply run the blocking IO on the IO dispatcher using withContext(Dispatchers.IO):

suspend fun write(byteArray: ByteArray) = withContext(Dispatchers.IO) {    
    bos.write(byteArray)
}

However, you have to think about which level you're using the IO dispatcher at. If this method is quite low-level, maybe you should use withContext higher in the call stack, and just keep this method non-suspend.

over 4 years ago · Santiago Trujillo Relatório

0

OutputStream.write is a blocking function, and by convention suspend functions must never block. You can wrap it in withContext so it uses the IO dispatcher to do it appropriately. However, it is possible this won't make the warning go away because the Kotlin lint is kind of buggy about false positives for this issue.

suspend fun write(byteArray: ByteArray) = withContext(Dispatchers.IO) {    
    bos.write(byteArray)
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda