英文:
Unresolved reference: Transformations after upgrading lifecycle dependency
问题
升级了生命周期依赖项从2.6.0-alpha04
到2.6.0-beta01
后,我遇到了"Unresolved reference: Transformations"的问题,无法导入androidx.lifecycle.Transformations
类。
import androidx.lifecycle.Transformations
...
var myList: LiveData<List<Bookmark>> = Transformations.switchMap(
bookMarkType
) { input: Int? ->
when (input) {
ARTICLE_BOOKMARK -> return@switchMap repository.articleBookmarks
WEBSITE_BOOKMARK -> return@switchMap repository.websiteBookmarks
LINK_BOOKMARK -> return@switchMap repository.linkBookmarks
}
repository.websiteBookmarks
}
英文:
After upgrading the lifecycle dependency from 2.6.0-alpha04
to 2.6.0-beta01
I got Unresolved reference: Transformations and it can't import androidx.lifecycle.Transformations
class.
import androidx.lifecycle.Transformations
...
var myList: LiveData<List<Bookmark>> = Transformations.switchMap(
bookMarkType
) { input: Int? ->
when (input) {
ARTICLE_BOOKMARK -> return@switchMap repository.articleBookmarks
WEBSITE_BOOKMARK -> return@switchMap repository.websiteBookmarks
LINK_BOOKMARK -> return@switchMap repository.linkBookmarks
}
repository.websiteBookmarks
}
答案1
得分: 73
截至2.6.0-alpha05版本:
> Transformations
现在由Kotlin编写。对于那些直接使用诸如Transformations.map
这样的语法的Kotlin类,这是一个源不兼容的更改 - Kotlin代码现在必须使用先前仅在使用lifecycle-livedata-ktx
时才可用的Kotlin扩展方法语法。在使用Java编程语言时,那些接受androidx.arch.core.util.Function
方法的方法的版本被弃用,并替换为接受Kotlin Function1
的版本。
因此,不要使用Transformations
,而是直接使用扩展函数myLiveData.switchMap
或myLiveData.map
。
要解决此问题,请使用:
var myList: LiveData<List<Bookmark>> = bookMarkType.switchMap { input: Int? ->
when (input) {
ARTICLE_BOOKMARK -> return@switchMap repository.articleBookmarks
WEBSITE_BOOKMARK -> return@switchMap repository.websiteBookmarks
LINK_BOOKMARK -> return@switchMap repository.linkBookmarks
}
repository.websiteBookmarks
}
英文:
As of 2.6.0-alpha05 version:
> Transformations is now written in Kotlin. This is a source incompatible change for those classes written in Kotlin that were directly using syntax such as Transformations.map - Kotlin code must now use the Kotlin extension method syntax that was previously only available when using lifecycle-livedata-ktx. When using the Java programming language, the versions of these methods that take an androidx.arch.core.util.Function method are deprecated and replaced with the versions that take a Kotlin Function1.
So, instead of using Transformations
, you need to use the extension function directly myLiveData.switchMap
or myLiveData.map
So, to fix this use:
var myList: LiveData<List<Bookmark>> = bookMarkType.switchMap { input: Int? ->
when (input) {
ARTICLE_BOOKMARK -> return@switchMap repository.articleBookmarks
WEBSITE_BOOKMARK -> return@switchMap repository.websiteBookmarks
LINK_BOOKMARK -> return@switchMap repository.linkBookmarks
}
repository.websiteBookmarks
}
答案2
得分: 3
[Zain] 给出了一个出色的答案,但为了进一步概括,以便为试图使用Transformations.map()将一种类型的LiveData转换为另一种类型而遇到"未解决的引用..."问题的人提供帮助。
如果你有一个类型为'T'的LiveData,并希望使用Transformations.map()将它转换为类型'X'的LiveData,那么现在可以这样做(在Lifecycle_version >= 2.6.0的情况下)。
val oldTypeLiveData: LiveData<T> = ...
val newTypeLiveData: LiveData<X> = oldTypeLiveData.map {
...传递你的lambda表达式以提供转换的实现
}
这里有一个关于Android开发者的参考链接。
英文:
Zain has given a fantastic answer, but just to elaborate and generalise for someone trying to convert one type of LiveData to another using Transformations.map() and getting "Unresolved reference..." issue.
If you have a LiveData of type 'T', and you wanted to convert it to LiveData of type 'X' using Transformations.map(), here's what you can do now(as of Lifecycle_version >= 2.6.0)
val oldTypeLiveData : LiveData<T> = ...
val newTypeLiveData : LiveData<X> = oldTypeLiveData.map{
...pass your lambda to provide implementation for the transformation
}
Here's a reference to Android Developers
答案3
得分: -1
当使用Java编程语言时,请使用 `kotlin.jvm.functions`: `Function0`, `Function1`, `Function2`, `Function3`, ...
import androidx.lifecycle.Transformations;
import kotlin.jvm.functions.Function1;
.....
**并将此替换为**
LiveData<List<TEntityRecord>> list = Transformations.switchMap(keyTime,
new Function1<Long, LiveData<List<TEntityRecord>>>() {
....
@Override
public LiveData<List<TEntityRecord>> invoke(Long mtime) {
return repository.getList(mtime);
}
});
英文:
When using the Java programming language, use kotlin.jvm.functions
: Function0
, Function1
, Function2
, Function3
, ...
import androidx.lifecycle.Transformations;
import kotlin.jvm.functions.Function1;
.....
and replace this
LiveData<List<TEntityRecord>> list = Transformations.switchMap(keyTime,
new Function<Long, LiveData<List<TEntityRecord>>>() {
....
});
with this
LiveData<List<TEntityRecord>> list = Transformations.switchMap(keyTime,
new Function1<Long, LiveData<List<TEntityRecord>>>() {
....
@Override
public LiveData<List<TEntityRecord>> invoke(Long mtime) {
return repository.getList(mtime);
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论