What is the best way to not use allowmainthreadqueries() using Android Room?

為{幸葍}努か 提交于 2021-02-10 14:14:50

问题


I always use allowmainthreadqueries() in Android Room. But many people say it is not recommend to do it. With this function, Room is very easy to use. But if i should change my app to not using this function, How do I do that? Is there an effective way to do it?


回答1:


Yes, It is not a good approach to query DB on Main Thread. There are different ways, to keep the DB operation off the main(UI) thread, Mainly

  1. Kotlin Coroutines. https://developer.android.com/codelabs/kotlin-android-training-coroutines-and-room#0
  2. RxJava(RxAndroid) https://medium.com/androiddevelopers/room-rxjava-acb0cd4f3757
  3. LiveData https://developer.android.com/codelabs/android-training-livedata-viewmodel#0
  4. Executors
  5. AsycTask

Out of the above, the best option would always depend upon what is your use case. But the first three are the best options to use. since they provide a callback on the result of a query.



来源:https://stackoverflow.com/questions/65642957/what-is-the-best-way-to-not-use-allowmainthreadqueries-using-android-room

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!