标签: android-livedata-transformations

未解决的参考:升级生命周期依赖项后的转换

将生命周期依赖项从 升级到 后,2.6.0-alpha042.6.0-beta01得到了未解决的参考:转换并且它无法import 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
        }
Run Code Online (Sandbox Code Playgroud)

android android-livedata android-livedata-transformations

33
推荐指数
2
解决办法
1万
查看次数

LiveData 转换地图功能

最近我一直在学习 LiveData 上的转换方法

我知道我们可以使用maporswitchMap方法来转换实时数据。假设我们有类似下面的 Player 数据类

data class Player(val name: String, val score: Int = 0)
Run Code Online (Sandbox Code Playgroud)

我们使用map方法将player livedata转换为playerName livedata

val player: LiveData<Player> = ...

val playerName: LiveData<String> = 
    Transformations.map(player) { it.name }
Run Code Online (Sandbox Code Playgroud)

我的问题是,在观察者函数中执行它有什么区别,因为它们都在主线程中运行?我的意思是,如果我们想获取playerName,那么我们也可以在观察者函数中获取它。为什么我们声明第二个 LiveData 实例来获取它

我从本指南中获取了示例代码:https://proandroiddev.com/livedata-transformations-4f120ac046fc

kotlin android-livedata mutablelivedata mediatorlivedata android-livedata-transformations

4
推荐指数
1
解决办法
6540
查看次数