26

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
        }
Zain
  • 37,492
  • 7
  • 60
  • 84
  • A `var` is used here as this is mutable in another context that not needed to mention here; so just in this context here it could be `val` normally – Zain Feb 28 '23 at 23:55
  • If there is a Class of PageViewModel like this , see this https://stackoverflow.com/a/76396193/12272687 – Mori Jun 03 '23 at 12:54

3 Answers3

68

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
        }
Zain
  • 37,492
  • 7
  • 60
  • 84
3

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

Abhishek Guru
  • 483
  • 4
  • 8
-1

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);
              }
        });
Milan
  • 1,743
  • 2
  • 13
  • 36
Raphael M
  • 11
  • 2