Rxjava 常用操作符

2020-04-14  本文已影响0人  醉了俗身醒了初心

Rxjava (Flowable, Observable, Single, Maybe, Completable)

1. amb 或者 ambArray 操作符

Single.ambArray(
                Single.just(1000),
                Single.just(100),
                Single.just(10)
            ).subscribe { t, e ->
                Log.d("test::", "${t.toString()}")
                Log.d("test::", e?.localizedMessage?:"")
            }
Log: 
2020-04-14 21:45:07.472 27021-27021/com.wwf.tipsy D/test::: 1000
Single.ambArray(
                Single.error(IllegalStateException("IllegalStateException")),
                Single.just(1000),
                Single.just(10)
            ).subscribe({
                Log.d("test::", it.toString())
            }, {
                L
Log.d("test::", it?.localizedMessage?:"")
            })
Log: 
2020-04-14 21:49:20.834 28349-28349/com.wwf.tipsy D/test::: IllegalStateException
Single.ambArray(
                Single.just(1000).delay(1000, TimeUnit.MILLISECONDS),
                Single.just(10).delay(300, TimeUnit.MILLISECONDS),
                Single.error(IllegalStateException("IllegalStateException"))
            ).subscribe({
                Log.d("test::", it.toString())
            }, {
                Log.d("test::", it?.localizedMessage?:"")
            })
Log:
2020-04-14 21:52:54.750 29028-29028/com.wwf.tipsy D/test::: IllegalStateException

contat 操作符

Single.concat(
                Single.just(1000)
                    .delay(1000, TimeUnit.MILLISECONDS)
                    .map {
                        10
                    },
                Single.just(10)
                    .delay(300, TimeUnit.MILLISECONDS)
                    .map {
                        20
                    },
                Single.timer(500, TimeUnit.MILLISECONDS).map {
                    30
                }
            ).subscribe({
                Log.d("test::", it.toString())
            }, {
                Log.d("test::", it?.localizedMessage ?: "")
            })
Log:

2020-04-14 22:06:12.548 30226-30481/com.wwf.tipsy D/test::: 10
2020-04-14 22:06:12.850 30226-30482/com.wwf.tipsy D/test::: 20
2020-04-14 22:06:13.353 30226-30483/com.wwf.tipsy D/test::: 30
Single.concat(
                Single.just(1000)
                    .delay(1000, TimeUnit.MILLISECONDS)
                    .map {
                        10L
                    },
                Single.error(RuntimeException("RuntimeException")),
                Single.timer(500, TimeUnit.MILLISECONDS).map {
                    30L
                }
            ).subscribe({
                Log.d("test::", it.toString())
            }, {
                Log.d("test::", it?.localizedMessage ?: "")
            })
Log:

2020-04-14 22:24:24.263 32226-32481/com.wwf.tipsy D/test::: 10
2020-04-14 22:24:24.265 32226-32481/com.wwf.tipsy D/test::: RuntimeException

zip 操作符

 Single.zip<Long, Long, Long, String>(
                Single.just(1000)
                    .delay(1000, TimeUnit.MILLISECONDS)
                    .map {
                        10L
                    },
                Single.just(10)
                    .delay(300, TimeUnit.MILLISECONDS)
                    .map {
                        20L
                    },
                Single.timer(500, TimeUnit.MILLISECONDS).map {
                    30L
                }, Function3 { t1, t2, t3 ->
                    "$t1 - $t2 - $t3"
                }
            ).subscribe({
                Log.d("test::", it.toString())
            }, {
                Log.d("test::", it?.localizedMessage ?: "")
            })
Log: 

2020-04-14 22:20:19.608 31170-31619/com.wwf.tipsy D/test::: 10 - 20 - 30
Single.zip<Long, Long, Long, String>(
                Single.just(1000)
                    .delay(1000, TimeUnit.MILLISECONDS)
                    .map {
                        10L
                    },
                Single.error(RuntimeException("RuntimeException")),
                Single.timer(500, TimeUnit.MILLISECONDS).map {
                    30L
                }, Function3 { t1, t2, t3 ->
                    "$t1 - $t2 - $t3"
                }
            ).subscribe({
                Log.d("test::", it.toString())
            }, {
                Log.d("test::", it?.localizedMessage ?: "")
            })
Log:

2020-04-14 22:22:10.322 31808-31808/com.wwf.tipsy D/test::: RuntimeException

merge 合并

val dispose =  Single.merge(
            Single.just(1000)
                .delay(1000, TimeUnit.MILLISECONDS)
                .map {
                    10L
                },
            Single.timer(300, TimeUnit.MILLISECONDS).map {
                20L
            },
            Single.timer(500, TimeUnit.MILLISECONDS).map {
                30L
            }
        ).subscribe({
            Log.d("test::", it.toString())
        }, {
            Log.d("test::", it?.localizedMessage ?: "")
        })
Log:

2020-04-14 22:31:26.743 1563-1723/com.wwf.tipsy D/test::: 20
2020-04-14 22:31:26.943 1563-1724/com.wwf.tipsy D/test::: 30
2020-04-14 22:31:27.442 1563-1722/com.wwf.tipsy D/test::: 10
val dispose = Single.merge(
            Single.just(1000)
                .delay(1000, TimeUnit.MILLISECONDS)
                .map {
                    10L
                },
            Single.error(RuntimeException("RuntimeException")),
            Single.timer(500, TimeUnit.MILLISECONDS).map {
                30L
            }
        ).subscribe({
            Log.d("test::", it.toString())
        }, {
            Log.d("test::", it?.localizedMessage ?: "")
        })
Log:

2020-04-14 22:35:23.573 2074-2074/com.wwf.tipsy D/test::: RuntimeException

subscribeWith

//可以统一处理生命周期和对话框

open class CommonSubscriber<T>(
    val context: Context,
    val consumer: ((T) -> Unit)? = null,
    val throwable: ((Throwable?) -> Unit)? = null
) : DisposableSubscriber<T>() {

    private val progressDialog by lazy { ProgressDialog() }

    override fun onStart() {
        super.onStart()
        progressDialog.showDialog(context)
    }

    override fun onComplete() {
        progressDialog.dismissDialog()
    }

    override fun onNext(t: T) {
        progressDialog.dismissDialog()
        consumer?.invoke(t)
    }

    override fun onError(t: Throwable?) {
        throwable?.invoke(t)
        progressDialog.dismissDialog()
    }
}

val dispose = Single.merge(
            Single.just(1000)
                .delay(3000, TimeUnit.MILLISECONDS)
                .map {
                    10L
                }.subscribeOn(Schedulers.io()),
            Single.timer(300, TimeUnit.MILLISECONDS).map {
                20L
            }.subscribeOn(Schedulers.io()),
            Single.timer(500, TimeUnit.MILLISECONDS).map {
                30L
            }.subscribeOn(Schedulers.io())
        ).subscribeWith(CommonSubscriber<Long>(this, {
            
        }, {

        }))
上一篇下一篇

猜你喜欢

热点阅读