其他分享
首页 > 其他分享> > Operation之其他实用操作符

Operation之其他实用操作符

作者:互联网

delay

Observable.of(1,2,3,4)
    .delay(5, scheduler: MainScheduler.instance)
    .subscribe(onNext: { print($0) })
    .disposed(by: bag)

delaySubscription

Observable.of(1,2,3,4)
    .delaySubscription(3, scheduler: MainScheduler.instance)
    .subscribe(onNext: { print($0) })
    .disposed(by: bag)

materialize

Observable.of(1,2,3,4)
    .materialize()
    .subscribe(onNext: { print($0) })
    .disposed(by: bag)

dematerialize

Observable.of(1,2,3,4)
    .materialize()
    .dematerialize()
    .subscribe(onNext: { print($0) })
    .disposed(by: bag)

timeout

let times = [
["value": 1, "time": 0],
["value": 2, "time": 0.5],
["value": 3, "time": 1.0],
["value": 4, "time": 1.5],
["value": 5, "time": 5],
["value": 6, "time": 6]]

Observable.from(times)
    .flatMap { item in
        return Observable.of(Int(item["value"]!))
            .delaySubscription(Double(item["time"]!), scheduler: MainScheduler.instance)
}
    .timeout(2, scheduler: MainScheduler.instance) // 超过2秒没有发出元素, 则产生error事件
    .subscribe(onNext: { print($0) }, one rror: {print($0)})
    .disposed(by: bag)

标签:materialize,Observable,onNext,value,实用,操作符,time,print,Operation
来源: https://www.cnblogs.com/gchlcc/p/11975457.html