实时数据: 候选人决议将很快改变

在同样的情况下,当我调用 LiveData对象的 observe方法时,Android 工作室会显示警告消息

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

考生决议即将更改,请使用完全合格 名称显式调用下列更接近的候选项:

公共场所开放 Fun Observer (owner: LifcycleOwner,Observer: Observer < in Int? >) : Unit 定义于 androidx.life 循环

据我所知,这是在更新了 Kotlin 1.4之后发生的 这到底意味着什么?

11927 次浏览

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.

I tried remove the "import androidx.lifecycle.observe" and converted our observers like the following:

// From this
livedata.observe(viewLifecycleOwner, Observer<MyClass?> {
// do stuff with it
})


// To this
livedata.observe(viewLifecycleOwner) {
// do stuff with it
}

The issue occured when we need to remove the observer inside the onChange call, e.g.

    // From this
livedata.observe(viewLifecycleOwner, object : Observer<MyClass?> {
override fun onChanged(t: MyClass?) {
// Do stuff with it t
livedata.removeObserver(this)
}
})
    

// To this
livedata.observe(viewLifecycleOwner) {
// do stuff with it
livedata.removeObserver(this) <--- not working ofc
}

I've heard rumors that folks have broken out the observer as a variable and thus been able to add/remove it by referencing the variable. However I'm unable to figure out how to instantiate a local observer without using the "Observer<MyClass?> syntax.

In my case I used Kotlin 1.4.31 in buildgradle(project level) file.

 ext {
kotlin_version = '1.4.31'
}


dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

The warning/deprecation error was gone.

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 ->
                

}

The same issue occurred my code base too. The kotlin version is '1.4.32'

After removing below import sentiment, the warning disappeared.

import androidx.lifecycle.observe

If you are observing LiveData in ViewModel and encountered this issue then you might need to call owner::getLifecycle instead.

class MyViewModel: ViewModel(), DefaultLifecycleObserver {
 

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)


repository.myAwesomeMethod().observe(owner) {
// This will show warning.
}
}
}

Now replace owner with owner::getLifecycle and you are good.

class MyViewModel: ViewModel(), DefaultLifecycleObserver {
 

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)


repository.myAwesomeMethod().observe(owner::getLifecycle) {
// Do your things
}
}
}