create
ReactiveX:
You can create an Observable from scratch by using the Create operator. You pass this operator a function that accepts the observer as its parameter. Write this function so that it behaves as an Observable — by calling the observer’s onNext, onError, and onCompleted methods appropriately.
create
通过该操作符,你可以自己创造一个可观察者。
let ob = Observable<Int>.create { ov in
ov.onNext(1)
ov.onNext(2)
ov.onNext(3)
ov.onNext(4)
ov.onCompleted()
return Disposables.create()
}
ob.subscribe(onNext: { element in
print(element)
}).disposed(by: bag)
输出:
1
2
3
4