Kotlin:对可变类型的内部变量的不可变类型的只读访问



在Android中学习ViewModels时,出现了一个问题,感觉Kotlin就是要解决的。在下面的代码中,我们可以看到 MutableLiveData 值被用于编辑值和指标。但是,我们不希望这些可变值暴露给任何其他内容,特别是 Android 生命周期的成员。我们确实希望 Android 生命周期成员有权读取值,但不能设置它们。因此,下面显示的 3 个公开函数属于 LiveData<>不可变类型。

是否有更简单或更简洁的方法来公开可在内部编辑的只读值?这似乎是 Kotlin 要避免的:样板冗长。

class HomeListViewModel: ViewModel(){
    //Private mutable data
    private val repositories = MutableLiveData<List<Repo>>()
    private val repoLoadError = MutableLiveData<Boolean>()
    private val loading = MutableLiveData<Boolean>()

    //Exposed uneditable LIveData
    fun getRepositories():LiveData<List<Repo>> = repositories
    fun getLoadError(): LiveData<Boolean> = repoLoadError
    fun getLoadingStatuses(): LiveData<Boolean> = loading
    init{...//Do some stuff to MutableLiveData<>
    }
}

可能类似的非 Android 场景是:

class ImmutableAccessExample{
    private val theThingToBeEditedInternally = mutableListOf<String>()
    fun theThingToBeAccessedPublicly(): List<String> = theThingToBeEditedInternally
    init {
        theThingToBeEditedInternally.add(0, "something")
    }
}
我不知道

是否有可能避免冗长。但是,我以前见过,它通常被声明为财产。

private val _repositories = MutableLiveData<List<Repo>>()
val repositories : LiveData<List<Repo>> 
    get() = _repositories

这是约定,请参阅支持属性的名称中的文档

如果类有两个在概念上相同的属性,但一个是公共 API 的一部分,另一个是实现细节,请使用下划线作为私有属性名称的前缀:

遵循这篇文章的想法:

class HomeListViewModel: ViewModel(){
    val repositories: LiveData<List<Repo>> = MutableLiveData()
    init {
        repositories as MutableLiveData
        ...//Do some stuff to repositories
    }
}

我还没有找到解决这个问题的任何优雅解决方案,但这就是我的处理方式。

private val selectedPositionLiveData = MutableLiveData<Int>()
fun getSelectedPosition() = selectedPositionLiveData as LiveData<Int>

View通过公共getter方法进行观察,无需在ViewModel中定义第二个成员。由于我的 Java 背景和显式 getter,我可能更喜欢这种方法,但在我看来,这与其他任何解决方法一样干净简洁。

val 没有二传手,因为它是只读的,但如果你想要一个 var,你可以这样做

var repositories = MutableLiveData<List<String>>()
    private set
var repoLoadError = MutableLiveData<Boolean>()
    private set
var loading = MutableLiveData<Boolean>()
    private set

这将为您提供一个私人二传手和一个公共获取者

最新更新