How to combine two live data one after the other?

You can use my helper method: val profile = MutableLiveData<ProfileData>() val user = MutableLiveData<CurrentUser>() val title = profile.combineWith(user) { profile, user -> “${profile.job} ${user.name}” } fun <T, K, R> LiveData<T>.combineWith( liveData: LiveData<K>, block: (T?, K?) -> R ): LiveData<R> { val result = MediatorLiveData<R>() result.addSource(this) { result.value = block(this.value, liveData.value) } result.addSource(liveData) { result.value = … Read more

Android Room Persistence library and Kotlin

Usually in project build.gradle I define the dependencies versions: ext { buildToolsVersion = ‘25.0.2’ supportLibVersion = ‘25.3.1’ espressoVersion = ‘2.2.2’ archRoomVersion = ‘1.0.0-alpha1’ } so in app build.gradle the dependencies look like: dependencies { compile fileTree(dir: ‘libs’, include: [‘*.jar’]) compile “org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version” compile “com.android.support:appcompat-v7:${rootProject.supportLibVersion}” compile “android.arch.persistence.room:runtime:${rootProject.archRoomVersion}” annotationProcessor “android.arch.persistence.room:compiler:${rootProject.archRoomVersion}” kapt “android.arch.persistence.room:compiler:${rootProject.archRoomVersion}” androidTestCompile(“com.android.support.test.espresso:espresso-core:${rootProject.espressoVersion}”, { exclude group: ‘com.android.support’, module: … Read more

LiveData remove Observer after first callback

There is a more convenient solution for Kotlin with extensions: fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer<T>) { observe(lifecycleOwner, object : Observer<T> { override fun onChanged(t: T?) { observer.onChanged(t) removeObserver(this) } }) } This extension permit us to do that: liveData.observeOnce(this, Observer<Password> { if (it != null) { // do something } }) So to answer … Read more

Room : LiveData from Dao will trigger Observer.onChanged on every Update, even if the LiveData value has no change

There is simple solution in Transformations method distinctUntilChanged.expose new data only if data was changed. In this case we get data only when it changes in source: LiveData<YourType> getData(){ return Transformations.distinctUntilChanged(LiveData<YourType> source)); } But for Event cases is better to use this: https://stackoverflow.com/a/55212795/9381524

How to save enum field in the database room?

You can make a convert to each enum, like this: class Converters { @TypeConverter fun toHealth(value: String) = enumValueOf<Health>(value) @TypeConverter fun fromHealth(value: Health) = value.name } Or if you prefer store it as SQL integer, you can use ordinal too: class Converters { @TypeConverter fun toHealth(value: Int) = enumValues<Health>()[value] @TypeConverter fun fromHealth(value: Health) = value.ordinal … Read more