可监听序列

2020-07-05  本文已影响0人  fanren

一、创建可监听序列

能发出三种结果

let observable = Observable<Int>.create { (observer) -> Disposable in
    observer.onNext(1)
    observer.onNext(2)
    observer.onNext(4)
    observer.onCompleted()
    return Disposables.create()
}
        
observable.subscribe(onNext: { (value) in
    print(value)
}).dispose()

二、signle

能发出两种事件

let single = Single<Int>.create { (observer) -> Disposable in
    observer(.success(1))
    return Disposables.create()
}
single.subscribe(onSuccess: { (value) in
    print(value)
}, onError: { (_) in
            
}).dispose()

三、Completable

能发出两种事件

let completeable = Completable.create { (observer) -> Disposable in
    observer(.completed)
    return Disposables.create()
}
completeable.subscribe(onCompleted: {
    print("success")
}, onError: { (_) in
    print("error")
}).dispose()

四、Maybe

只能发出一种时间

let maybe = Maybe<Int>.create { (observer) -> Disposable in
    observer(.success(0))
    return Disposables.create()
}
        
maybe.subscribe(onSuccess: { (value) in
    print(value)
 }, onError: { (_) in
    print("error")
}, onCompleted: {
    print("complete")
}).dispose()
上一篇下一篇

猜你喜欢

热点阅读