- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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())
}
}
}
=== 更新 =====
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)
}
}
还有一个问题:
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/
我有两个问题: 'bind(onNext:' 和 'subscribe(onNext:' 有什么区别? struct Info { var index: Int? var data:
在一个使用 RxJava 2 的 Android 项目中,我在初始 Activity 的 onCreate 中创建了一个像这样的 Flowable: Flowable.create(new MyFlo
我有一个函数可以从网络服务中获取一些信息: const rxjs = require('rxjs'); const request = require('request'); const moment
所以我有一个Observable。我想知道什么时候 每个 下游订阅者 onNext 调用将要进行,这既是为了测量每个下游订阅者花费的时间量,也是为了背压目的。 Observable.lift 让我完成
我是 Rx 的新手,我真的很感激在错误处理方面的一些帮助。我有以下代码,它基本上是预先输入的: var observable = Rx.Observable.fromEvent(searchField
有没有办法延迟某些值的onNext?,例如: let subject = new Rx.BehaviorSubject({value: 100}) subject.onNext({ value: 12
下面我有向两个 API 端点发出请求的代码。我正在寻找一种让 onNext() 函数仅触发一次的方法。现在,如果您运行下面的代码,您可以看到它 logs fire 1,然后是 fire 2。我只想让它
我有一个发出长值的可观察值。一旦我收到一个>0的长值,那么我只需要中断发出并点击onComplete。有没有比我所做的黑客方法更好的方法来做到这一点 long id = -1l; methodA()
我正在尝试使用 RxJava 创建模型的代理,该代理允许其他人订阅模型中的更改。 这是我的代理的简化版本: class MyModelProxy { private static MyMode
我正在用 Retrofit 调用一个 url,我不关心结果。无论如何要忽略 onNext 方法吗? 目前我正在使用: getRetrofit().create(UserAPI.class)
我正在使用 RxAndroid 库来处理使用订阅者/可观察模式的项目列表。我的问题是,当一个项目被处理时,有一个进度条需要更新。但是在处理了 16 个项目之后,似乎 observable 没有调用 o
我有一个创建新 Observable 的用例: Observable.create(new Observable.OnSubscribe() { @Override public void
PublishSubject 非常适合在观察者和可观察对象之间架起一座桥梁: Observable observable = Observable.just("string"); Pu
有没有更简洁的方法来执行以下操作? 我有一个 Android observable 可以消除请求。 onNext 它调用第二个可观察对象。 {// when creating the android
我想在调用 onNext 后 10 秒发出一个随机值。所以 onNext 将被调用 12345,10 秒后 onNext 应该被调用以获得一个随机数。使用 RxJava 实现此目的的最佳方法是什么?提
每隔一段时间我都会用特定的查询检索推文。这些推文必须传递给计算和操作这些推文的服务。所以这些服务订阅了我的出版商。所以 publisher.hasSubscribers() 返回 true。但是提交或
我正在尝试在我的 http 代码中使用一些 reactiveX。为此,我创建了一个主题,并尝试调用 onNext 函数,但我收到一条错误消息,指出 subject.onNext 不是一个函数。 htm
我有这个简单的 retrofit2 api 接口(interface),其中包含 interface Api { @GET(BuildConfig.END_POINT) Observable
有人可以告诉我这个插件是如何工作的 http://bootstraptour.com/api/ ?我有一个步骤,我想重定向到动态网址。所以我编写了 onNext 回调,如下所示的代码。重定向工作正常,
我在 GRPC 文档中找不到任何与此相关的内容。 GRPC 是否期望我的 StreamObserver.onNext() 实现是非阻塞的?如果 GRPC 确实阻塞(例如拒绝新请求、对新请求排队等),会
我是一名优秀的程序员,十分优秀!