在Android Room中不使用allowmainthreadqueries()的最佳方法是什么? [英] What is the best way to not use allowmainthreadqueries() using Android Room?

查看:436
本文介绍了在Android Room中不使用allowmainthreadqueries()的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是在Android Room中使用allowmainthreadqueries().但是很多人说不建议这样做. 有了此功能,Room非常易于使用. 但是,如果我应该将我的应用程序更改为不使用此功能,该怎么办? 有有效的方法吗?

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?

推荐答案

是的,这不是在主线程上查询数据库的好方法.有多种方法可以使数据库操作远离main(UI)线程,主要是

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. 科特琳·协程. https://developer.android.com/codelabs/kotlin-android-training-协程和房间#0
  2. RxJava(RxAndroid) https://medium.com/androiddevelopers/room-rxjava-acb0cd4f3757
  3. LiveData https://developer.android.com/codelabs/android-training-livedata-viewmodel#0
  4. 执行人
  5. AsycTask
  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.

这篇关于在Android Room中不使用allowmainthreadqueries()的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆