RxSwift

官方文档结构

  1. Introduction:
  2. Subjects
  3. Transforming Observables
  4. Filtering Observables
  5. Combining Observables
  6. Error Handing Operators
  7. Observable Utility Operators
  8. Conditional and Boolean Operators
  9. Mathematical and Aggregate Operators
  10. Connectable Observable Operatiors

Observable

Observable 是观察者模式中被观察的对象,相当于一个事件序列(GeneratorType), 会向订阅者发送新产生的事件信息。

事件信息分为:

.Next(value): 表示新的事件数据
.Completed: 表示事件序列完结
.Error: 异常导致的事件序列完结

Subjects

A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable.

Subject可以看做是一种代理和桥梁。它既是订阅者又是订阅源,这意味着它既可以订阅其他Observables,同时又可以对他的订阅者发送事件

1. How To Create Observables ?

  1. empty:

    empty creates an empty sequence.
    The only message it sends is the .Completed message. let emptySequence = Observable<Int>.empty()
  2. never:

    never creates a sequence that never sends any element or completes.
    
    let neverSequence = Observable<Int>.never()
  3. just (一个元素)

    just represents sequence that contains one element.
    It sends two messages to subscribers.
    The first message is the value of single element and the second
    message is .Completed. let singleElementSequence = Observable.just(32)
  4. sequenceOf (一系列元素)

    sequenceOf creates a sequence of a fixed number of elements.
  5. from (将swift的序列(SequenceType) 转换为事件序列)

    from creates a sequence from SequenceType
    
    let sequenceFromArray = [1, 2, 3, 4, 5].toObservable()
  6. create (通过闭包创建序列)

    create creates sequence using Swift closure.
    This examples creates custom version of just operator. let myJust = { (singleElement: Int) -> Observable<Int> in
    return Observable.create { observer in
    observer.on(.Next(singleElement))
    observer.on(.Completed) return NopDisposable.instance
    }
    } let subscription = myJust(5)
    .subscribe { event in
    print(event)
    }
  7. generate

    generate creates sequence that generates its values and determines
    when to terminate based on its previous values. let generated = Observable.generate(
    initialState: 0,
    condition: { $0 < 3 },
    iterate: { $0 + 1 }
    ) let subscription = generated
    .subscribe { event in
    print(event)
    }
  8. failWith

    create an Observable that emits no items and terminates with an error
    
    let error = NSError(domain: "Test", code: -1, userInfo: nil)
    let erroredSequence = Observable<Int>.error(error)
  9. deferred (加载延迟, 订阅者的内容相同而完全独立)

    do not create the Observable until the observer subscribes,
    and create a fresh Observable for each observer

2. How To Create Subjects

  1. PublishSubject(发送订阅者从订阅之后的事件序列)

    PublishSubject emits(发出) to an observer only those items that
    are emitted by the source Observable(s) subsequent to the
    time of the subscription. example("PublishSubject") {
    let disposeBag = DisposeBag() let subject = PublishSubject<String>()
    writeSequenceToConsole("1", sequence: subject).addDisposableTo(disposeBag)
    subject.on(.Next("a"))
    subject.on(.Next("b"))
    writeSequenceToConsole("2", sequence: subject).addDisposableTo(disposeBag)
    subject.on(.Next("c"))
    subject.on(.Next("d"))
    }
    1 - a
    1 - b
    1 - c
    2 - c
    1 - d
    2 - d
  2. ReplaySubject (在新订阅对象订阅的时候会补发所有已经发送过的数据列, buffize: 是缓冲区的大小, 为1时,那么新订阅者出现的时候就补发上一个事件,如果是2, 就补发2个 ,…)

    ReplaySubject emits to any observer all of the items that
    were emitted by the source Observable(s), regardless of
    when the observer subscribes. example("ReplaySubject") {
    let disposeBag = DisposeBag()
    let subject = ReplaySubject<String>.create(bufferSize: 1) writeSequenceToConsole("1", sequence: subject).addDisposableTo(disposeBag)
    subject.on(.Next("a"))
    subject.on(.Next("b"))
    writeSequenceToConsole("2", sequence: subject).addDisposableTo(disposeBag)
    subject.on(.Next("c"))
    subject.on(.Next("d"))
    }
    print:
    1 - a
    1 - b
    2 - b // 补发1个
    1 - c
    2 - c
    1 - d
    2 - d
  3. BehaviorSubject (在新的订阅对象订阅的时候回发送最近发送的事件,如果没有,则发送一个默认值)

    When an observer subscribes to a BehaviorSubject, it begins
    by emitting the item most recently emitted by the source
    Observable (or a seed/default value if none has yet been emitted)
    and then continues to emit any other items emitted later
    by the source Observable(s). example("BehaviorSubject") {
    let disposeBag = DisposeBag() let subject = BehaviorSubject(value: "z")
    writeSequenceToConsole("1", sequence: subject).addDisposableTo(disposeBag)
    subject.on(.Next("a"))
    subject.on(.Next("b"))
    writeSequenceToConsole("2", sequence: subject).addDisposableTo(disposeBag)
    subject.on(.Next("c"))
    subject.on(.Next("d"))
    subject.on(.Completed)
    }
    print:
    1 - z
    1 - a
    1 - b
    2 - b
    1 - c
    2 - c
    1 - d
    2 - d
    1 - com
    2 - com
  4. Variable (是基于BehaviorSubject的一层封装, 它的优势: 不会被显示的终结,即:不会受到.Complete或者.Error这类终结事件,它会主动在析构的时候发送.Complete)

    Variable wraps BehaviorSubject. Advantage of using variable over
    BehaviorSubject is that variable can never explicitly complete
    or error out, and BehaviorSubject can in case Error or Completed
    message is send to it. Variable will also automatically complete
    in case it"s being deallocated. example("Variable") {
    let disposeBag = DisposeBag()
    let variable = Variable("z")
    writeSequenceToConsole("1", sequence: variable.asObservable()).addDisposableTo(disposeBag)
    variable.value = "a"
    variable.value = "b"
    writeSequenceToConsole("2", sequence: variable.asObservable()).addDisposableTo(disposeBag)
    variable.value = "c"
    variable.value = "d"
    }

3. Transforming Observables

  1. map/select

More info in reactive.io website

example("map") {
let originalSequence = Observable.of(1, 2, 3) _ = originalSequence
.map { number in
number * 2
}
.subscribe { print($0) }
}

RxSwift 之官方文档的更多相关文章

  1. 【AutoMapper官方文档】DTO与Domin Model相互转换(上)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  2. 2DToolkit官方文档中文版打地鼠教程(三):Sprite Collections 精灵集合

    这是2DToolkit官方文档中 Whack a Mole 打地鼠教程的译文,为了减少文中过多重复操作的翻译,以及一些无必要的句子,这里我假设你有Unity的基础知识(例如了解如何新建Sprite等) ...

  3. 2DToolkit官方文档中文版打地鼠教程(二):设置摄像机

    这是2DToolkit官方文档中 Whack a Mole 打地鼠教程的译文,为了减少文中过多重复操作的翻译,以及一些无必要的句子,这里我假设你有Unity的基础知识(例如了解如何新建Sprite等) ...

  4. 2DToolkit官方文档中文版打地鼠教程(一):初始设置

    这是2DToolkit官方文档中 Whack a Mole 打地鼠教程的译文,为了减少文中过多重复操作的翻译,以及一些无必要的句子,这里我假设你有Unity的基础知识(例如了解如何新建Sprite等) ...

  5. 【AutoMapper官方文档】DTO与Domin Model相互转换(中)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  6. 【AutoMapper官方文档】DTO与Domin Model相互转换(下)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  7. Ionic2系列——Ionic 2 Guide 官方文档中文版

    最近一直没更新博客,业余时间都在翻译Ionic2的文档.之前本来是想写一个入门,后来觉得干脆把官方文档翻译一下算了,因为官方文档就是最好的入门教程.后来越翻译越觉得这个事情确实比较费精力,不知道什么时 ...

  8. Kotlin开发语言文档(官方文档)-- 目录

    开始阅读Kotlin官方文档.先上文档目录.有些内容还未阅读,有些目录标目翻译还需琢磨琢磨.后续再将具体内容的链接逐步加上. 文档链接:https://kotlinlang.org/docs/kotl ...

  9. 一起学微软Power BI系列-官方文档-入门指南(1)Power BI初步介绍

    我们在前一篇文章微软新神器-Power BI,一个简单易用,还用得起的BI产品中,我们初步介绍了Power BI的基本知识.由于Power BI是去年开始微软新发布的一个产品,虽然已经可以企业级应用, ...

随机推荐

  1. ajax不进success,

    $.ajax({ url:"/order/pay_order_wx?order_id="+order_id, type:'GET', data:"{}", da ...

  2. JS学习之路,菜鸟总结的注意事项及错误更正

    JavaScript 是一种面向对象的动态语言,它的语法来源于 Java 和 C,所以这两种语言的许多语法特性同样适 用于 JavaScript.需要注意的一个主要区别是 JavaScript 不支持 ...

  3. Storm 集群安装配置

    本文详细介绍了 Storm 集群的安装配置方法.如果需要在 AWS 上安装 Storm,你应该看一下 storm-deploy 项目.storm-deploy 可以自动完成 E2 上 Storm 集群 ...

  4. Twitter Storm源代码分析之Nimbus/Supervisor本地目录结构

    storm集群里面工作机器分为两种一种是nimbus, 一种是supervisor, 他们通过zookeeper来进行交互,nimbus通过zookeeper来发布一些指令,supervisor去读z ...

  5. [ An Ac a Day ^_^ ] UVALive 7270 Osu! Master

    2015icpc北京区域赛的签到题 还是很签到的 一次就过了 题意呢 就是统计B C后最长上升序列还有S的个数 当然B C要被S分割开…… /* *************************** ...

  6. css-文本垂直居中(转)

    css-文本垂直居中(转) 在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在 ...

  7. windows cmd启动heritrix

    首先下载heritrix-1.14.4.zip文件,可以在http://sourceforge.net/projects/archive-crawler/files/heritrix3/下载.下载后解 ...

  8. redis数据类型:lists

    redis的list类型其实就是一个每个子元素都是string类型的双向链表. 我们可以通过push,pop操作从链表的头部或者尾部添加删除元素,这样list即可以作为 栈,又可以作为队列. lpus ...

  9. Android上的远程调试

    来源: http://www.seejs.com/archives/296 目录 远程调试概述 使用 Chrome 的 ADB 扩展进行远程调试 1. 安装 ADB 扩展 2. 启用你的移动设备上的 ...

  10. [原]innerText与innerHTML区别

    window.onload = function () {             document.getElementById('btn1').onclick = function () {    ...