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?