(*useful)标记:目前觉得有用的函数
//FIXME 标记:待补充
toArray: 将可观察序列转换为数组,将该数组作为新的单个元素可观察序列发出
example("toArray") {
let disposeBag = DisposeBag()
Observable.range(start: 1, count: 10)
.toArray()
.subscribe { print($0) }
.disposed(by: disposeBag)
}
打印:
next([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
completed
reduce 累加器,将所有的累加,然后作为一个Observable sequence emit消息
example("reduce") {
let disposeBag = DisposeBag()
Observable.of(10, 100, 1000)
.reduce(7, accumulator: +)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
打印:
--- reduce example ---
1117
concat:(*useful)
合并队列,按加入顺序排列,只有上一个sequences发出了onCompleted后,下一个sequences才能Emit
example("concat") {
let disposeBag = DisposeBag()
let subject1 = BehaviorSubject(value: "🍎")
let subject2 = BehaviorSubject(value: "🐶")
let variable = Variable(subject1)
variable.asObservable()
.concat()
.subscribe { print($0) }
.disposed(by: disposeBag)
subject1.onNext("🍐")
subject1.onNext("🍊")
variable.value = subject2
subject2.onNext("I would be ignored")
subject2.onNext("🐱")
subject1.onCompleted()
subject2.onNext("🐭")
}
打印:
next(🍎)
next(🍐)
next(🍊)
next(🐱) //这个不该被输出的,但是这使用了BehaviorSubject(会发送最近的一次emit)
next(🐭)
网友评论