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. 自定义MVC框架(二) -基于XML文件

    1.oracle的脚本 create table STUDENT ( sid NUMBER primary key, sname ), age NUMBER, pwd ) ) create seque ...

  2. stack(STL)

    //Stack STL //在STL中,栈是以别的容器作为底部结构,再将 //接口改变,使之符合栈的特性 //一共5个常用操作函数 //构造析构 stack<Elem>c; //build ...

  3. 高精度运算专题1-加法运算(The addition operation)

    这个专题呢,我就来讲讲高精度的加法,下面是一个计算加法的函数(用数组a加上数组b结果存到数组c里面). 思路:先测一下数组a和数组b的长度,分别放到a[0].b[0]里面去,再从第二位开始相加,记得满 ...

  4. 一个初学者的辛酸路程-初识Python-1

    前言 很喜欢的一句话,与诸位共勉. 人的一切痛苦,本质上都是对自己无能的愤怒----王小波. 初识Python 一.它的爸爸是谁 首先,我们需要认识下面这位人物. 他是Python的创始人,吉多范罗苏 ...

  5. php 关了浏览器也可以自动运行脚本

    <?php ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行. set_time_limit(0); //执行时间为无限制,php默认 ...

  6. CRC的校验原理

    一.基本原理 CRC检验原理实际上就是在一个p位二进制数据序列之后附加一个r位二进制检验码(序列),从而构成一个总长为n=p+r位的二进制序列:附加在数据序列之后的这个检验码与数据序列的内容之间存在着 ...

  7. 如何在VBS脚本中显示“选择文件对话框”或“选择目录对话框”

    .选择文件[XP操作系统,不能用于Win2000或98],使用“UserAccounts.CommonDialog”对象向用户显示一个标准的“文件打开”对话框 Set objDialog = Crea ...

  8. MFC中spin control使用

    1.绑定spin和edit m_Spin.SetBuddy(GetDlgItem(m_Edit1)); m_Spin.SetRange(0,100); 2.实现数值的增减 双击控件添加消息 void ...

  9. hdu_3709_Balanced Number(数位DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题意:给你一个区间,让你找平衡数的个数 题解:设dp[i][j][k]为前i位以第j位为支撑点的 ...

  10. C++primer 9.43

    题目要求:编写一个函数,接受三个string参数s,oldVal和newVal.使用迭代器及insert和erase函数将s中所有oldVal替换为newVal.测试你的程序,用它替换通用的简写形式, ...