LiveData from architecture components defines an Observer with nullable value for the receiver callback:
public interface Observer<T> {
/**
* Called when the data is changed.
* @param t The new data
*/
void onChanged(@Nullable T t);
}
Why is there an explicitly nullable annotation?
The doc of LiveData.observe()
also says:
If LiveData already has data set, it will be delivered to the observer.
E.g. Observer waits for non-nullable updates or immediately receive previous non-nullable value, that should hold especially in Kotlin, until I define T
as nullable.
The code seem to be working like that. I understand why this doesn't hold for LiveData.getValue()
, which may be called manually before first data is delivered (and checks therefore for mData != NOT_SET
to return a null).
So the second question is: Is is safe to assume the value is non-null in Kotlin when T is non-nullable?
LiveData is written in Java. It does allow setting it's value to null . So the authors could add @Nullable annotation as we have them on other Jetpack libraries, right? No, as then you always need to handle nullable types in getValue or the Observer although you clearly might have declared your LiveData as non-null.
Why should you initialize and store LiveData in your ViewModel instead of a UI Controller? Both the ViewModel and LiveData are lifecycle aware. To ensure that the LiveData isn't destroyed when the UI Controller is destroyed. To hide or separate implementation details making your app more flexible.
LiveData notifies Observer objects when underlying data changes. You can consolidate your code to update the UI in these Observer objects. That way, you don't need to update the UI every time the app data changes because the observer does it for you. No memory leaks.
MutableLiveData is just a class that extends the LiveData type class. MutableLiveData is commonly used since it provides the postValue() , setValue() methods publicly, something that LiveData class doesn't provide.
I think the fact that they made it Nullable is that they wanted to add the functionality for those who want to reset
the liveData by nulling it's value. Also someone might want a nullable LiveData (and use that null in observe).
If you are creating/producing the LiveData, you could assume it is null (and use the !!
operator) since a null would indicate an unexpected error. Also you could create a class like NonNullLiveData that ignores the values that are null in it's setValue
. That way you could be sure that you never recieve nulls in your observe (although you can't make the @Nullable
go away from the observer).
Fixed in androix.lifecycle 2.0.0-beta01
.
Please report android Team if you encounter any issues.
Starting from 2.0.0-beta01
and later androidx.lifecycle
version the onChanged
parameter no longer contains @Nullable
annotation. The changes is due to the improvement request courtesy by the OP.
package androidx.lifecycle;
/**
* A simple callback that can receive from {@link LiveData}.
*
* @param <T> The type of the parameter
*
* @see LiveData LiveData - for a usage description.
*/
public interface Observer<T> {
/**
* Called when the data is changed.
* @param t The new data
*/
void onChanged(T t);
}
Is is safe to assume the value is non-null in Kotlin when T is non-nullable?
It depends, if you're the one who created the LiveData
subclass or plainly using the existing MutableLiveData
and you design it that it will never return null then it is safe to assume that it will never return null.
For the case that the LiveData
is implicitly created by not you especially those LiveData
that are provided by Libraries, I would not assume it is non-null unless the library documentation mentioned it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With