Created
March 19, 2018 10:57
-
-
Save Audhil/d20640f975b2c4a035b2d154a2e21f0d to your computer and use it in GitHub Desktop.
converting Realm Data to LiveData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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