Rxjava (Flowable, Observable, Single, Maybe, Completable)
1. amb 或者 ambArray 操作符
- 当你传递多个Observable给amb操作符时,该操作符只发射其中一个Observable的数据和通知:首先发送通知给amb操作符的的那个Observable,不管发射的是一项数据还是一个onError或onCompleted通知,amb将忽略和丢弃其它所有Observables的发射物,
- 既是amb操作符,谁先调用onNext, 就先执行谁。
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 操作符
-
按顺序一个一个的执行
image.png
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 操作符
-
image.png
在同一个线程时, 会按照执行顺序合并
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 合并
-
conat 和 merge 之间的区别在于,merge 不等待当前可观察到的终止后才转移到下一个。merge 订阅它所能看到的每一个可观察到的项,并在它们出现时发出项, 如果遇到异常, 后面的都不会执行
2020-04-14_225738.png -
与其他静态方法的组合程序一样,merge 有一个替代方法,允许您在链中逐个合并序列。该运算符称为mergewith,其行为是相同的。下面的示例与上面的示例具有相同的结果。
-
对于 merge,只要任何源序列失败,合并序列也会失败。该行为的另一个替代方法是MergeDelayError,它将延迟错误的释放,并继续合并没有失败的序列中的值。
2020-04-14_225927.png
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, {
}, {
}))
网友评论