Search code examples
javaandroidandroid-livedataandroid-lint

Is there a better way than self-assign value for the Livedata to update view?


For Android's LiveData, I sometimes use self-assignment so that my views can be updated.

For example:

fun retry() {
    myModel.setLoadingRetry()
    loadStatesMap
        .forEach {
            it.value.first?.value = t.value.first?.value
        }
    retryCallback?.invoke()
}

However, it makes Sonarqube code quality bug, since it it not usual. Bug description:

There is no reason to re-assign a variable to itself. Either this statement is redundant and should be removed, or the re-assignment is a mistake and some other value or variable was intended for the assignment instead.

I guess I can update views by not self-assigning my Livedata value. If anyone have an idea, please let me know.


Solution

  • I could call Observer:onChanged(T) instead of self-assigning value.