未解决的引用:升级生命周期依赖后的转换

huangapple go评论56阅读模式
英文:

Unresolved reference: Transformations after upgrading lifecycle dependency

问题

升级了生命周期依赖项从2.6.0-alpha042.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&lt;List&lt;Bookmark&gt;&gt; = Transformations.switchMap(
            bookMarkType
        ) { input: Int? -&gt;
            when (input) {
                ARTICLE_BOOKMARK -&gt; return@switchMap repository.articleBookmarks
                WEBSITE_BOOKMARK -&gt; return@switchMap repository.websiteBookmarks
                LINK_BOOKMARK -&gt; 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.switchMapmyLiveData.map

要解决此问题,请使用:

var myList: LiveData&lt;List&lt;Bookmark&gt;&gt; = bookMarkType.switchMap { input: Int? -&gt;
            when (input) {
                ARTICLE_BOOKMARK -&gt; return@switchMap repository.articleBookmarks
                WEBSITE_BOOKMARK -&gt; return@switchMap repository.websiteBookmarks
                LINK_BOOKMARK -&gt; 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&lt;List&lt;Bookmark&gt;&gt; = bookMarkType.switchMap { input: Int? -&gt;
            when (input) {
                ARTICLE_BOOKMARK -&gt; return@switchMap repository.articleBookmarks
                WEBSITE_BOOKMARK -&gt; return@switchMap repository.websiteBookmarks
                LINK_BOOKMARK -&gt; 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&lt;T&gt; = ...

val newTypeLiveData : LiveData&lt;X&gt; = 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&lt;List&lt;TEntityRecord&gt;&gt; list = Transformations.switchMap(keyTime,
            new Function1&lt;Long, LiveData&lt;List&lt;TEntityRecord&gt;&gt;&gt;() {
            ....
               @Override
               public LiveData&lt;List&lt;TEntityRecord&gt;&gt; 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&lt;List&lt;TEntityRecord&gt;&gt; list = Transformations.switchMap(keyTime,
        new Function&lt;Long, LiveData&lt;List&lt;TEntityRecord&gt;&gt;&gt;() {
        ....
        });

with this

 LiveData&lt;List&lt;TEntityRecord&gt;&gt; list = Transformations.switchMap(keyTime,
        new Function1&lt;Long, LiveData&lt;List&lt;TEntityRecord&gt;&gt;&gt;() {
        ....
           @Override
           public LiveData&lt;List&lt;TEntityRecord&gt;&gt; invoke(Long mtime) {
              return repository.getList(mtime);
              }
        });

huangapple
  • 本文由 发表于 2023年2月16日 05:06:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定