九、可连接的操作-Connectable Operators(Rx.playground翻译)

注意:使用本例中的代码首先应该导入头文件,代码如下:

import RxSwift

Connectable Operators

可连接的Observable序列类似于普通的Observable序列,除了当它们被订阅后一开始不发散元素,只有当它们的connect方法被调用才会发散元素。这样,你可以等待所有的订阅者在可连接的Observable序列开始发散元素之前订阅它。

在本页的每个例子中都有一个注释掉的方法。�运行例子需要取消注释,停止运行需要注释掉。

在学习关于可连接的Observable序列的知识之前,让我看一个不是可连接的�操作:

func sampleWithoutConnectableOperators() {
    printExampleHeader(#function)

    let interval = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
    _ = interval
        .subscribeNext { print("Subscription: 1, Event: \($0)") }

    delay(5) {
        _ = interval
            .subscribeNext { print("Subscription: 2, Event: \($0)") }
    }
}
//sampleWithoutConnectableOperators() //  Uncomment to run this example; comment to stop running

在指定的调度程序之上,interval(间隔)创建一个Observable序列并且在每个period(周期)之后发散元素。了解更多


publish

把源Observable序列转换成一个可连接的Observable序列。�了解更多

func sampleWithPublish() {
    printExampleHeader(#function)

    let intSequence = Observable<Int>.interval(1, scheduler: MainScheduler.instance)

    _ = intSequence
        .subscribeNext { print("Subscription: 1, Event: \($0)") }

    delay(2) { intSequence.connect() }

    delay(4) {
        _ = intSequence
            .subscribeNext { print("Subscription: 2, Event: \($0)") }
    }

    delay(6) {
        _ = intSequence
            .subscribeNext { print("Subscription: 3, Event: \($0)") }
    }
}

//sampleWithPublish() //  Uncomment to run this example; comment to stop running

调度程序�作为一个抽象机制来执行工作,比如在特定线程或调度队列。�了解等多


replay

把源Observable序列转换成一个可连接的Observable序列,并且将重复�发散bufferSize次。�了解更多

func sampleWithReplayBuffer() {
    printExampleHeader(#function)
    
    let intSequence = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
        .replay(5)
    
    _ = intSequence
        .subscribeNext { print("Subscription 1:, Event: \($0)") }
    
    delay(2) { intSequence.connect() }
    
    delay(4) {
        _ = intSequence
            .subscribeNext { print("Subscription 2:, Event: \($0)") }
    }
    
    delay(8) {
        _ = intSequence
            .subscribeNext { print("Subscription 3:, Event: \($0)") }
    }
}

// sampleWithReplayBuffer() //  Uncomment to run this example; comment to stop running

multicast

把源Observable序列转换成一个可连接的Observable序列,并通过指定的subject广播发散。

func sampleWithMulticast() {
    printExampleHeader(#function)
    
    let subject = PublishSubject<Int>()
    
    _ = subject
        .subscribeNext { print("Subject: \($0)") }
    
    let intSequence = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
        .multicast(subject)
    
    _ = intSequence
        .subscribeNext { print("\tSubscription 1:, Event: \($0)") }
    
    delay(2) { intSequence.connect() }
    
    delay(4) {
        _ = intSequence
            .subscribeNext { print("\tSubscription 2:, Event: \($0)") }
    }
    
    delay(6) {
        _ = intSequence
            .subscribeNext { print("\tSubscription 3:, Event: \($0)") }
    }
}

//sampleWithMulticast() // Uncomment to run this example; comment to stop running

下一篇: ��十、����错误处理操作-Error Handling Operators(Rx.playground翻译)�

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • 本篇文章介主要绍RxJava中操作符是以函数作为基本单位,与响应式编程作为结合使用的,对什么是操作、操作符都有哪些...
    嘎啦果安卓兽阅读 2,890评论 0 10
  • 作者: maplejaw本篇只解析标准包中的操作符。对于扩展包,由于使用率较低,如有需求,请读者自行查阅文档。 创...
    maplejaw_阅读 45,769评论 8 93
  • 生活中常想去做很多事情。但往往因为没有坚持下来而放弃。 是什么导致没有坚持下来呢?是动力。当失去了做一件事的动力的...
    金铭123阅读 215评论 0 0