Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the value param nullable in Observer from Android Architecture Components?

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?

like image 774
hrach Avatar asked May 11 '18 12:05

hrach


People also ask

Can LiveData be 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?

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.

Why do we need LiveData?

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.

What is mutable live data?

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.


3 Answers

  1. 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).

  2. 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).

like image 185
Adib Faramarzi Avatar answered Sep 30 '22 09:09

Adib Faramarzi


Fixed in androix.lifecycle 2.0.0-beta01.

Please report android Team if you encounter any issues.

like image 45
Prags Avatar answered Sep 30 '22 11:09

Prags


Starting from 2.0.0-beta01 and later androidx.lifecycle version the onChangedparameter 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.

like image 36
Enzokie Avatar answered Sep 30 '22 09:09

Enzokie