实时数据:候选人分辨率将很快更改
Live Data: Candidate resolution will be changed soon
在相同的情况下 Android 当我调用 LiveData
对象的 observe
方法时工作室显示警告消息
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
据我了解,这是在更新 kotlin 1.4 后发生的
它到底是什么意思?
表示androidx中的扩展已经不需要了
只需删除其导入 import androidx.lifecycle.observe
。
在 androidx 中 will be actually deprecated。在那里阅读更多推理。
编辑:
请注意 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.
我很好奇他们是否会重命名并保留助手或提出其他解决方案。
我尝试删除“import androidx.lifecycle.observe”并像下面这样转换我们的观察者:
// From this
livedata.observe(viewLifecycleOwner, Observer<MyClass?> {
// do stuff with it
})
// To this
livedata.observe(viewLifecycleOwner) {
// do stuff with it
}
当我们需要在 onChange 调用中移除观察者时出现问题,例如
// 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
}
我听说有人将观察者分解为一个变量,因此能够通过引用该变量来 add/remove 它。但是,我无法弄清楚如何在不使用 "Observer 语法的情况下实例化本地观察者。
就我而言,我在构建 gradle(项目级别)文件中使用了 Kotlin 1.4.31。
ext {
kotlin_version = '1.4.31'
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
warning/deprecation 错误消失了。
AndroidX 扩展已弃用,因此我们必须使用原始扩展。
删除import androidx.lifecycle.observe
然后
viewModel.liveData.observe(viewLifecycleOwner, Observer { result ->
})
或
viewModel.liveData.observe(viewLifecycleOwner) { result ->
}
我的代码库也出现了同样的问题。 kotlin 版本是 '1.4.32'
删除下面的导入情绪后,警告消失了。
import androidx.lifecycle.observe
如果您在 ViewModel
中观察 LiveData
并遇到此问题,那么您可能需要改为调用 owner::getLifecycle
。
class MyViewModel: ViewModel(), DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
repository.myAwesomeMethod().observe(owner) {
// This will show warning.
}
}
}
现在用 owner::getLifecycle
替换 owner
就可以了。
class MyViewModel: ViewModel(), DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
repository.myAwesomeMethod().observe(owner::getLifecycle) {
// Do your things
}
}
}
在相同的情况下 Android 当我调用 LiveData
对象的 observe
方法时工作室显示警告消息
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
据我了解,这是在更新 kotlin 1.4 后发生的 它到底是什么意思?
表示androidx中的扩展已经不需要了
只需删除其导入 import androidx.lifecycle.observe
。
在 androidx 中 will be actually deprecated。在那里阅读更多推理。
编辑:
请注意 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.
我很好奇他们是否会重命名并保留助手或提出其他解决方案。
我尝试删除“import androidx.lifecycle.observe”并像下面这样转换我们的观察者:
// From this
livedata.observe(viewLifecycleOwner, Observer<MyClass?> {
// do stuff with it
})
// To this
livedata.observe(viewLifecycleOwner) {
// do stuff with it
}
当我们需要在 onChange 调用中移除观察者时出现问题,例如
// 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
}
我听说有人将观察者分解为一个变量,因此能够通过引用该变量来 add/remove 它。但是,我无法弄清楚如何在不使用 "Observer
就我而言,我在构建 gradle(项目级别)文件中使用了 Kotlin 1.4.31。
ext {
kotlin_version = '1.4.31'
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
warning/deprecation 错误消失了。
AndroidX 扩展已弃用,因此我们必须使用原始扩展。
删除import androidx.lifecycle.observe
然后
viewModel.liveData.observe(viewLifecycleOwner, Observer { result ->
})
或
viewModel.liveData.observe(viewLifecycleOwner) { result ->
}
我的代码库也出现了同样的问题。 kotlin 版本是 '1.4.32'
删除下面的导入情绪后,警告消失了。
import androidx.lifecycle.observe
如果您在 ViewModel
中观察 LiveData
并遇到此问题,那么您可能需要改为调用 owner::getLifecycle
。
class MyViewModel: ViewModel(), DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
repository.myAwesomeMethod().observe(owner) {
// This will show warning.
}
}
}
现在用 owner::getLifecycle
替换 owner
就可以了。
class MyViewModel: ViewModel(), DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
repository.myAwesomeMethod().observe(owner::getLifecycle) {
// Do your things
}
}
}