gpt4 book ai didi

kotlin 在 onNext 或不应为 null 的 BehaviorSubject 中传递 mutableList

转载 作者:行者123 更新时间:2023-12-02 11:54:32 25 4
gpt4 key购买 nike

Kotlin 1.3.72,
RxJava2
我有以下代码,我试图避免使用 !!运算符,但不确定为什么它认为该值为 null,我需要使用安全调用运算符。
以后我得用!!这是不好的做法。为什么这会是空的,因为我已经声明了任何可以为空的类型?

class SharedViewModel : ViewModel() {
private val compositeDisposable = CompositeDisposable()
private val imageSubject = BehaviorSubject.create<MutableList<Photo>>()
private val selectedPhotos = MutableLiveData<List<Photo>>()

init {
imageSubject.subscribeBy {
selectedPhotos.value = it
}.addTo(compositeDisposable)
}

fun getSelectedPhotos(): LiveData<List<Photo>> {
return selectedPhotos
}

fun addPhotos(photo: Photo) {
// Not sure why I need the safe-call operator here
imageSubject.value?.add(photo)
// Using the !! is bad practice and would like to avoid it
imageSubject.onNext(imageSubject.value!!)

// This is how I am currently handling it, but just wondering why the value would be null when it is not a nullable type?
if(imageSubject.value != null) {
imageSubject.onNext(imageSubject.value ?: mutableListOf())
}
}
}
=== 更新 =====
我做了一些更改和更新。我的最后一个使用 let 。
fun addPhotos(photo: Photo) {
imageSubject.value?.add(photo)

// Original
imageSubject.onNext(imageSubject.value!!)

// First Attempt
if(imageSubject.value != null) {
imageSubject.onNext(imageSubject.value ?: mutableListOf())
}

// Final Attempt
imageSubject.value?.let {
imageSubject.onNext(it)
}
}
还有一个问题:
将某些内容添加到 BehaviourSubject imageSubject.value?.add(photo) 中是一种好习惯吗?然后立即使用 onNext imageSubject.onNext(it) 发出该值?

最佳答案

BehaviourSubject可以有空值,因为它打算在订阅时发出默认值(这是它内部的最新值,如果不存在则为空)如果您不想要这种方法,请使用 PublishSubject .
至于使用当前类型的实现,我会做类似的事情

class SharedViewModel : ViewModel() {
private val compositeDisposable = CompositeDisposable()
//It is better to use singular object streams instead of list publish
private val imageSubject = BehaviorSubject.create<Photo>()
//It is better to use singular object livedata instead of list post. Collect it into a list in place where you subscribe to it or use approach recommended by @Franz Andel above
private val _selectedPhotos = MutableLiveData<Photo>()
//Use this approach instead of explicit getter function
val selectedPhotos: LiveData<Photo>
get() = _selectedPhotos

init {
imageSubject.subscribeBy {
_selectedPhotos.postValue(it)
}.addTo(compositeDisposable)
}

fun addPhotos(photo: Photo) {
imageSubject.onNext(photo)
}
}
一些建议:
//Do not do it like that with subjects - do not add something to their value directly.
imageSubject.value?.add(photo)
// Using the !! is bad practice and would like to avoid it
imageSubject.onNext(imageSubject.value!!)
回答如何在没有 !! 的情况下以最佳方式处理可空类型:
//This piece of code is pointless but it has needed context.
imageSubject.value?.apply{
imageSubject.onNext(this)
}
您也可能想使用 elvis operator :
imageSubject.onNext(imageSubject.value ?: someDefaultValue)

关于kotlin 在 onNext 或不应为 null 的 BehaviorSubject 中传递 mutableList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63338882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com