Reputation: 40908
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
}
Upvotes: 41
Views: 20792
Reputation: 613
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
Upvotes: 6
Reputation: 71
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);
}
});
Upvotes: 0
Reputation: 40908
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
}
Upvotes: 107