RxSwift如何映射到onComplete事件

时间:2018-05-21 13:17:27

标签: rx-swift

我有一个BehaviorSubject<[Int]>,数组的长度一次增加一个。我希望有Observable在第一个元素添加到数组时发出completed事件,我不关心事后会发生什么。怎么做?

1 个答案:

答案 0 :(得分:2)

BehaviorSubject的订阅包裹到您自己的Observable中,当您onCompleted的内容发生变化时,该BehaviorSubject会致电let array = BehaviorSubject<[Int]>(value: []) var observable: Observable<Void> { return Observable<Void>.create { observer in // skip first element as BehaviorSubject repeats last value on subscription array.skip(1).subscribe(onNext: { _ in observer.onCompleted() }) } }

observable.subscribe(onCompleted: {
    print("bump")
}).disposed(by: disposeBag)

array.onNext([1]) // now "bump" gets printed
array.onNext([1, 2])

然后像这样使用它:

THREE.Geometry