Rxjava - 用批处理迭代列表
Rxjava - Iterate the list with batch
我有一些字符串的列表,我需要在其中批量迭代列表。
示例:
val list = ["a","b","c","d","e","f"]
Observable.fromIteratable(list)
.map{
//here i need to get 4 items at a time
//like in first iteration i need to get ["a","b","c","d"]
//in second iteration i need to get ["e","f"]
}
是否有任何选项可以执行此操作?
用户buffer
periodically gather items emitted by an Observable into bundles and emit these bundles rather than emitting the items one at a time
val list = arrayOf("1", "2", "3", "4", "5")
Observable.fromIterable(list.asIterable())
.buffer(4)
.map { stringList: MutableList<String> ->
println("insideMap -> $stringList")
return@map "wow $stringList"
}
.subscribe { value: String -> println("OnResult $value")}
//Output
insideMap -> [1, 2, 3, 4]
OnResult wow [1, 2, 3, 4]
insideMap -> [5]
OnResult wow [5]
The Buffer operator transforms an Observable that emits items into an Observable that emits buffered collections of those items. There are a number of variants in the various language-specific implementations of Buffer that differ in how they choose which items go in which buffers.
注意,如果源 Observable 发出 onError 通知,Buffer 将立即传递此通知,而不会首先发出它正在组装的缓冲区,即使该缓冲区包含源 Observable 在发出错误通知之前发出的项目。
我有一些字符串的列表,我需要在其中批量迭代列表。
示例:
val list = ["a","b","c","d","e","f"]
Observable.fromIteratable(list)
.map{
//here i need to get 4 items at a time
//like in first iteration i need to get ["a","b","c","d"]
//in second iteration i need to get ["e","f"]
}
是否有任何选项可以执行此操作?
用户buffer
periodically gather items emitted by an Observable into bundles and emit these bundles rather than emitting the items one at a time
val list = arrayOf("1", "2", "3", "4", "5")
Observable.fromIterable(list.asIterable())
.buffer(4)
.map { stringList: MutableList<String> ->
println("insideMap -> $stringList")
return@map "wow $stringList"
}
.subscribe { value: String -> println("OnResult $value")}
//Output
insideMap -> [1, 2, 3, 4]
OnResult wow [1, 2, 3, 4]
insideMap -> [5]
OnResult wow [5]
The Buffer operator transforms an Observable that emits items into an Observable that emits buffered collections of those items. There are a number of variants in the various language-specific implementations of Buffer that differ in how they choose which items go in which buffers.
注意,如果源 Observable 发出 onError 通知,Buffer 将立即传递此通知,而不会首先发出它正在组装的缓冲区,即使该缓冲区包含源 Observable 在发出错误通知之前发出的项目。