kotlin使订户使用RxJava2观察可观察物 [英] kotlin getting a subscriber to observe an observable using RxJava2

查看:163
本文介绍了kotlin使订户使用RxJava2观察可观察物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Studio 3.0 Beta2

我创建了2种方法,一种创建可观察的方法,另一种创建订户.

I have created 2 methods one that creates the observable and another that creates the subscriber.

但是,我在尝试使订阅者订阅可观察对象时遇到问题.在Java中,这是可行的,而我正在尝试使其在Kotlin中可用.

However, I am having a issue try to get the subscriber to subscribe to the observable. In Java this would work, and I am trying to get it to work in Kotlin.

在我的onCreate(..)方法中,我试图进行设置.这是正确的方法吗?

In my onCreate(..) method I am trying to set this. Is this the correct way to do this?

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /* CANNOT SET SUBSCRIBER TO SUBCRIBE TO THE OBSERVABLE */
        createStringObservable().subscribe(createStringSubscriber())
    }


    fun createStringObservable(): Observable<String> {
        val myObservable: Observable<String> = Observable.create {
            subscriber ->
            subscriber.onNext("Hello, World!")
            subscriber.onComplete()
        }

        return myObservable
    }

    fun createStringSubscriber(): Subscriber<String> {
        val mySubscriber = object: Subscriber<String> {
            override fun onNext(s: String) {
                println(s)
            }

            override fun onComplete() {
                println("onComplete")
            }

            override fun onError(e: Throwable) {
                println("onError")
            }

            override fun onSubscribe(s: Subscription?) {
                println("onSubscribe")
            }
        }

        return mySubscriber
    }
}

非常感谢您的任何建议,

Many thanks for any suggestions,

推荐答案

请密切注意这些类型.

Observable.subscribe()具有三个基本变体:

  • 不接受任何参数的人
  • 几个接受io.reactivex.functions.Consumer
  • 的人
  • 接受io.reactivex.Observer
  • 的人
  • one that accepts no arguments
  • several that accept an io.reactivex.functions.Consumer
  • one that accepts an io.reactivex.Observer

在您的示例中尝试预订的类型是org.reactivestreams.Subscriber(定义为Reactive Streams Specification的一部分).您可以参考文档以获取文档对此类型进行了更全面的说明,但是足以说明它与任何重载的Observable.subscribe()方法都不兼容.

the type you're attempting to subscribe with in your example is org.reactivestreams.Subscriber (defined as part of the Reactive Streams Specification). you can refer to the docs to get a fuller accounting of this type, but suffice to say it's not compatible with any of the overloaded Observable.subscribe() methods.

这是您的createStringSubscriber()方法的修改示例,该示例将允许您的代码进行编译:

here's a modified example of your createStringSubscriber() method that will allow your code to compile:

fun createStringSubscriber(): Observer<String> {
        val mySubscriber = object: Observer<String> {
            override fun onNext(s: String) {
                println(s)
            }

            override fun onComplete() {
                println("onComplete")
            }

            override fun onError(e: Throwable) {
                println("onError")
            }

            override fun onSubscribe(s: Disposable) {
                println("onSubscribe")
            }
        }

        return mySubscriber
    }

发生的变化是:

  1. 这将返回Observer类型(而不是Subscriber)
  2. onSubscribe()传递了Disposable(而不是Subscription)
  1. this returns an Observer type (instead of Subscriber)
  2. onSubscribe() is passed a Disposable (instead of Subscription)

..正如"Vincent Mimoun-Prat"所提到的那样,lambda语法确实可以缩短您的代码.

.. and as mentioned by 'Vincent Mimoun-Prat', lambda syntax can really shorten your code.

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Here's an example using pure RxJava 2 (ie not using RxKotlin)
        Observable.create<String> { emitter ->
            emitter.onNext("Hello, World!")
            emitter.onComplete()
        }
                .subscribe(
                        { s -> println(s) },
                        { e -> println(e) },
                        {      println("onComplete") }
                )

        // ...and here's an example using RxKotlin. The named arguments help
        // to give your code a little more clarity
        Observable.create<String> { emitter ->
            emitter.onNext("Hello, World!")
            emitter.onComplete()
        }
                .subscribeBy(
                        onNext     = { s -> println(s) },
                        onError    = { e -> println(e) },
                        onComplete = {      println("onComplete") }
                )
    }

我希望有帮助!

这篇关于kotlin使订户使用RxJava2观察可观察物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆