Я использую Room
в своем проекте, но когда я использую insert
или update
, это медленно.
Мой код для Select
: (DialogFragment
):
class LangsListDialogFragment: DaggerAppCompatDialogFragment(), LangsListAdapter.LangListListener {
@BindView(R.id.rcvLangsFilter)
lateinit var rcvLangsFilter: RecyclerView
@Inject
lateinit var langListDialogViewModel: LangsListDialogViewModel
@Inject
lateinit var langsListAdapter: LangsListAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_dialog_langs_list, container);
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ButterKnife.bind(this, view)
val layoutManager = LinearLayoutManager(activity)
layoutManager.orientation = RecyclerView.VERTICAL
rcvLangsFilter.setLayoutManager(layoutManager)
rcvLangsFilter.addItemDecoration(
DividerItemDecoration(
activity,
DividerItemDecoration.VERTICAL
)
)
fillFromDao()
}
@OnClick(R.id.icBtnCloseLangsFilter)
fun icBtnCloseLangsFilterClick(){
dismiss()
}
private fun fillFromDao() {
val notesObserver: Observer<List<Language?>?>? =
Observer { langs: List<Language?>? ->
langsListAdapter.setLangs(langs)
rcvLangsFilter.adapter = langsListAdapter
}
if (notesObserver != null) {
langListDialogViewModel.fetchLangsModel()?.observe(this, notesObserver)
}
}
override fun onCheckBoxStatus(id_language: Int?, id: Int?, symbol: String?, isSelected: Boolean) {
langListDialogViewModel.updateChecked(id_language, id, symbol, isSelected)
}
}
И по моему ViewModel
:
class LangsListDialogViewModel @Inject constructor(application: Application) :
AndroidViewModel(application) {
private val executor: Executor =
Executors.newSingleThreadExecutor()
private val appRepository: AppRepository =
AppRepository.getInstance(application.applicationContext)!!
fun fetchLangsModel(): LiveData<List<Language?>?>? {
val callable = Callable { appRepository.selectLangsAppRepo() }
val future = Executors.newSingleThreadExecutor().submit(callable)
return future.get()
}
fun updateChecked(id_language: Int?, id: Int?, symbol: String?, isSelected: Boolean) {
executor.execute {
appRepository.updateCheckedAppRepo(id_language, id, symbol, isSelected)
}
}
}
И мой Repository
это:
class AppRepository private constructor(context: Context) {
private val appDatabase: AppDatabase? = AppDatabase.getInstance(context)
companion object {
private var ourInstance: AppRepository? = null
fun getInstance(context: Context): AppRepository? {
if (ourInstance == null) {
ourInstance = AppRepository(context)
}
return ourInstance
}
}
//region Langs List Dialog
fun selectLangsAppRepo(): LiveData<List<Language?>?>? {
return if (this.appDatabase?.coursesDao() != null) {
appDatabase.langsListDao()!!.langsDB()
} else {
null
}
}
fun updateCheckedAppRepo(id_language: Int?, id: Int?, symbol: String?, isSelected: Boolean){
if (this.appDatabase?.coursesDao() != null) {
this.appDatabase.langsListDao()!!.updateChecked(id_language, id, symbol, isSelected)
}
}
//endregion
}
А мой DAO
это:
@Dao
interface LangsListDao {
@Query("SELECT * FROM Language")
fun langsDB(): LiveData<List<Language?>?>?
@Query("UPDATE Language SET isSelectedLan = :isSelected WHERE id_language = :id_language AND id = :id AND symbol = :symbol")
fun updateChecked(id_language: Int?, id: Int?, symbol: String?, isSelected: Boolean)
}