Foggy day

Android(Kotlin) - RxKotlin -2 : Observable 본문

Android

Android(Kotlin) - RxKotlin -2 : Observable

jinhan38 2021. 3. 23. 22:33

 

        val source = Observable.create { emitter: ObservableEmitter<String?>? ->
            emitter?.onNext("Hello")
            emitter?.onNext("android")
            emitter?.onComplete()
        }.subscribe(::println)

//        source.subscribe(::println)

 

 

 

   val source = Observable.just("Hello", "android")
        source.subscribe(::println)

 

 

 

 val stringList = listOf<String>("A", "B", "C")
        val source = Observable.fromArray(stringList)
        source.subscribe(::println)

        val stringArrayList = arrayListOf<String>("D", "E", "F")
        val sourceSecond = Observable.fromIterable(stringArrayList)
        sourceSecond.subscribe(::println)

        val future = Executors.newSingleThreadExecutor().submit<String> {
            try {
                Thread.sleep(3000)
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
            "Hello android"
        }
        val sourceFuture = Observable.fromFuture(future)
        sourceFuture.subscribe(::println)


        val publisher = Publisher { subscriber: Subscriber<in String?> ->
            subscriber.onNext("A")
            subscriber.onNext("C")
            subscriber.onNext("E")
            subscriber.onComplete()
        }

        val sourcePublisher = Observable.fromPublisher(publisher)
        sourcePublisher.subscribe(::println)