A. If a `LiveData` object returns a non-null type ...
# android
s
A. If a
LiveData
object returns a non-null type would it at all trigger an observer's
onChange()
if there is no data to be served?
Copy code
val someLiveData: LiveData<List<String>> = ...
val someObserver = { someValue: String ->
    // Will this return only if there is non-null data in someLiveData
}
B. Same case as above except the observer class expects a nullable type. So if
LiveData
returns non-null type but the observer object expects a nullable type then would we get a null value when there's nothing to be returned?
Copy code
val someLiveData: LiveData<List<String>> = ...
val someObserver = { someValue: String? ->
   // Will this return null value from a non-null LiveData
}
a
Nullability declared in the generics for a LiveData instance have no effect on runtime behavior. LiveData performs no filtering of null values for observers; if java code sets a null value to the LiveData instance it will send a null to your observer, potentially causing a crash if you aren't expecting it.
LiveData considers the "not set" state on construction to be different from nullability of the value type. During this state, LiveData will return null from
getValue
but it will not dispatch a null to newly registered observers