RxSwift 操作符 (do)

do

ReactiveX:
You can register callbacks that ReactiveX will call when certain events take place on an Observable, where those callbacks will be called independently from the normal set of notifications associated with an Observable cascade. There are a variety of operators that various ReactiveX implementations have designed to allow for this.

注册一个操作符来执行可观察者的生命周期事件,它与subscribe是无关的,调用它并不影响susbscribe。

Observable<[Int]>.of([1, 2, 3])
    .do(onNext: { element in
        print("do element:" ,element)
    }, onError: { error in
        print("do error:", error)
    }, onCompleted: {
        print("do completed")
    }, onSubscribe: {
        print("do subscribe")
    }, onSubscribed: {
        print("do subscribed")
    }, onDispose: {
        print("do dispose")
    })
    .subscribe { event in
        switch event {
        case .next(let element):
            print("element:", element)
        case .error(let error):
            print("error:", error)
        case .completed:
            print("completed")
        }}
    .disposed(by: bag)

输出:
do subscribe
do subscribed
do element: [1, 2, 3]
element: [1, 2, 3]
do completed
completed
do dispose

do优先于subscribe打印。

阅读更多
个人分类: RxSwift
想对作者说点什么? 我来说一句

RxSwift - API

RxSwift

yangmeng13930719363 yangmeng13930719363

2016-07-14 07:22:43

阅读数:1573

RxSwift 中文文档 epub

2018年04月22日 13.73MB 下载

RxSwift 中文文档

2018年05月17日 13.73MB 下载

没有更多推荐了,返回首页

不良信息举报

RxSwift 操作符 (do)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭