1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Roomの処理をAcitivtyでしようとして「Cannot access database on the main thread since it may potentially lock the UI for a long period of time.」と出た時やったこと

Posted at

Roomの処理をAcitivtyでしようとして「Cannot access database on the main thread since it may potentially lock the UI for a long period of time.」と出た時やったこと

#参考
Kotlin で Room を使う際に詰まったところをまとめる
非同期処理のCoroutine(コルーチン)を始めてみよう ...
(上記のonDestroyでコルーチンのJobキャンセルし忘れがなくなり、記述も簡潔に書けるようになるとの記述がとても参考になりました。)

#結果
ScopedAppActivityを作成し各々のActivityにて継承してコルーチンを使用することにしました。


open class ScopedAppActivity: AppCompatActivity(), CoroutineScope by MainScope() {
    
    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }
}
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?