如何手动更新可观察对象?

How to update an observable manually?

我是 reactivex 和 rxscala 的新手,可以像这样创建 Observable

val observable = Observable[String] { subscriber => 
    subscriber.onNext("something")
}

我可以将新字符串放入 Observable.apply 中的 subscriber

可以在外面更新observable吗?我的意思是,有没有像 putNext:

这样的方法
observable.putNext("another string")

让我将新事物添加到现有的可观察对象中?

如果你想手动控制一个Observable,你需要使用一个Subject。

根据 ReactiveX documentation:

A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by reemitting them, and it can also emit new items.

您可以订阅一个 Subject,但您也可以将新元素传递给它,实际上是手动控制它,这就是您所要求的。

示例:

val subject = PublishSubject[String]()
subject.onNext("one")
subject.onNext("two")
subject.onNext("three")
subject.onCompleted()