Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live Data: Candidate resolution will be changed soon

In same cases Android studio displays warning message when I call observe method of LiveData object

viewModel.emailValidationResult.observe(viewLifecycleOwner, {onEmailChanged(it)}) 

Candidate resolution will be changed soon, please use fully qualified name to invoke the following closer candidate explicitly:

public open fun observe(owner: LifecycleOwner, observer: Observer<in Int?>): Unit defined in androidx.lifecycle.LiveData

As I understand it's happened after updating kotlin 1.4 What does it actually mean ?

like image 265
Vahe Gharibyan Avatar asked Sep 24 '20 11:09

Vahe Gharibyan


People also ask

Is LiveData deprecated?

This function is deprecated. This extension method is not required when using Kotlin 1.4.

What is live data and 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.

What is live data and observer?

LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.


2 Answers

It means that the extension in androidx is not needed anymore.

Simply remove its import import androidx.lifecycle.observe.

It will be actually deprecated in androidx. Read more reasoning there.

EDIT:

Please note the "issue" from Erik Hoogendoorn

This change causes values from the observed LiveData object to be interpreted as nullable(since the converted lambda syntax is based on nullable Java code). This was not the case for the Kotlin extension and causes a loss in functionality for the user. In my opinion this change should be reverted and a different solution found.

I'm curious if they will rename & retain the helper or come up with another solution.

like image 80
hrach Avatar answered Sep 27 '22 18:09

hrach


AndroidX extension is deprecated, so we have to use the original one.

Remove import androidx.lifecycle.observe

then

viewModel.liveData.observe(viewLifecycleOwner, Observer { result ->      }) 

or

viewModel.liveData.observe(viewLifecycleOwner) { result ->                  } 
like image 28
BollMose Avatar answered Sep 27 '22 17:09

BollMose