RxJava Observable.create 不为 switchIfEmpty

我正在试验 RxJava 运算符并遇到了这个问题,但不知道它为什么会这样。让我看一个例子,希望问题会很清楚。


我有一个使用 Observable.just 创建的水果 Observable


Observable<String> fruits = Observable.just("Apple", "Banana", "Mango", "Strawberry", "Raspberry", "Blood Oranges");

我使用 Observable.create 创建了另一种可观察的语言


Observable<String> pLanguages = Observable.create(emitter -> {

            emitter.onNext("Kotlin");

            emitter.onNext("Java");

            emitter.onNext("Python");

            emitter.onNext("Javascript");

            emitter.onNext("Go");

            emitter.onNext("C");

            emitter.onNext("Rust");

        });

switchIfEmpty() 操作符将订阅我们传递给它的操作符,如果源操作符没有发出任何 observables。


使用这个过滤器,我应该会看到基于 switchIfEmpty() 定义的结果。


pLanguages.filter(s -> s.startsWith("Z"))

                .switchIfEmpty(fruits)

                .subscribe(System.out::println, System.out::println, () -> System.out.println("Complete"));

但我没有看到任何排放。但是,如果我像这样切换 observables


fruits.filter(s -> s.startsWith("Z"))

                .switchIfEmpty(pLanguages)

                .subscribe(System.out::println, System.out::println, () -> System.out.println("Complete"));

我确实看到 pLanguages 观察者发出事件。我尝试调试但不确定为什么 Observable.create() 没有为 switchIfEmpty() 运算符发出空的 observable 而 Observable.just() 发出。我在这上面花了几个小时,希望这里有人能回答这个问题。


呼如林
浏览 150回答 1
1回答

哈士奇WWW

概括您需要在languages源上发出完成信号示例(基于提供的源代码)Observable<String> pLanguages = Observable.create(emitter -> {&nbsp; &nbsp; emitter.onNext("Kotlin");&nbsp; &nbsp; emitter.onNext("Java");&nbsp; &nbsp; emitter.onNext("Python");&nbsp; &nbsp; emitter.onNext("Javascript");&nbsp; &nbsp; emitter.onNext("Go");&nbsp; &nbsp; emitter.onNext("C");&nbsp; &nbsp; emitter.onNext("Rust");&nbsp; &nbsp; emitter.onComplete();});Kotlin 中的示例 (addt'l)fun main(args: Array<String>) {&nbsp; &nbsp; val numbers = Observable.just("one", "two", "three")&nbsp; &nbsp; val languages = Observable.create<String> { emitter ->&nbsp; &nbsp; &nbsp; &nbsp; emitter.onNext("java")&nbsp; &nbsp; &nbsp; &nbsp; emitter.onNext("kotlin")&nbsp; &nbsp; &nbsp; &nbsp; emitter.onNext("swift")&nbsp; &nbsp; &nbsp; &nbsp; emitter.onComplete() // <-- Invoke this&nbsp; &nbsp; }&nbsp; &nbsp; languages&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter { s -> s.toLowerCase().startsWith("z") }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .switchIfEmpty(numbers)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subscribe({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(it)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(it)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; numbers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter { s -> s.toLowerCase().startsWith("z") }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .switchIfEmpty(languages)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subscribe({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(it)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(it)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })}输出onetwothreejavakotlinswift阅读更多http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Emitter.html
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java