LoginSignup
7
3

More than 3 years have passed since last update.

Kotlinでコルーチンを使っていて、"Inappropriate blocking method call"という警告が出る場合

Posted at

対処法

  • Alt+Enter押して出る様に@Suppress("BlockingMethodInNonBlockingContext")を追加する
  • 対象の行を適切なコルーチンコンテキスト(大抵の場合はDispatchers.IO)で呼び出す

2個めの対処法の場合はwithContext(Dispatchers.IO) { blockingMethod() }ってするのが一番楽

原因

詳細知らないので推測ですが、呼び出しているメソッド(大抵の場合外部ライブラリ/JDK等の非suspendメソッド)でブロッキング処理(多くの場合I/O処理)が行われているのをIntelliJが検知して警告を出しているんだと思います。
I/O関係のブロッキング処理はIOコンテキストで呼び出せば大丈夫なので、コルーチンコンテキストは適切に使いましょう、ということですね。

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3