Skip to content

Instantly share code, notes, and snippets.

@Audhil
Created March 19, 2018 10:57
Show Gist options
  • Save Audhil/d20640f975b2c4a035b2d154a2e21f0d to your computer and use it in GitHub Desktop.
Save Audhil/d20640f975b2c4a035b2d154a2e21f0d to your computer and use it in GitHub Desktop.
converting Realm Data to LiveData
// realm live data
class RealmLiveData<T : RealmModel>(private val results: RealmResults<T>) : LiveData<RealmResults<T>>() {
private val listener = RealmChangeListener<RealmResults<T>> { results -> value = results }
override fun onActive() {
results.addChangeListener(listener)
}
override fun onInactive() {
results.removeChangeListener(listener)
}
}
************************************************
// DummyViewModel.kt
fun findCustomView(): RealmLiveData<GetCustomModuleRespo> {
return RealmLiveData(mRealm.where(GetCustomModuleRespo::class.java).findAllAsync())
}
******************************************************
// DummyActivity.kt
viewModel.findCustomView().observe(this, Observer {
it.let {
println (it)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment