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