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. STORM在线业务实践-集群空闲CPU飙高问题排查

    源:http://daiwa.ninja/index.php/2015/07/18/storm-cpu-overload/ 2015-07-18AUTHORDAIWA STORM在线业务实践-集群空闲 ...

  2. delphi怎么实现全选的功能

    1. SelectAll 可以实现全选功能 Delphi/Pascal code edit1.SelectAll; // Delphi/Pascal code RichEdit1.SelStart:= ...

  3. vultr vps官网改版免费注册教程(最新优惠码)

    本站强烈推荐的,国外超高性价比vps服务器Vultr官方网站最近更新了,导致一些朋友心生茫然,今天写个简易教程给大家. 首先是打开vultr官网,免费注册一个vultr账号.填写邮箱地址.密码,非常简 ...

  4. Arch下载官方镜像列表Official mirrors

    Official mirrors The official Arch Linux mirror list is available from the pacman-mirrorlist package ...

  5. 【Machine Learning in Action --5】逻辑回归(LogisticRegression)从疝气病预测病马的死亡率

    背景:使用Logistic回归来预测患有疝气病的马的存活问题,这里的数据包括368个样本和28个特征,疝气病是描述马胃肠痛的术语,然而,这种病并不一定源自马的胃肠问题,其他问题也可能引发疝气病,该数据 ...

  6. BJFU 1440 fudq的ACM

    矩阵快速幂 #include<cstdio> #include<algorithm> using namespace std; ; const int INF =1e9; ; ...

  7. SVN ---文件加锁,执行clean up命令

    一.SVN 中 clean up 的功能 当Subversion改变你的工作拷贝(或是.svn中的任何信息),它会尽可能的小心,在修改任何事情之前,它把意图写到日志文件中去,然后执行log文件中的命令 ...

  8. c语言结构体指针必须初始化

    先说结论 结构体指针需要初始化 结构体指针的成员指针同样需要初始化 结构体变量定义的时候就已经分配了内存空间,而上面两个确没有 struct test{ int i; struct buf *p;} ...

  9. ebtables

    ebtables是以太网桥防火墙,以太网工作在数据链路层,ebtables过滤数据链路层包.2.6内核内置了ebtables,要使用它必须先按装她的用户空间工具(ebtables-V2.0.6),安装 ...

  10. ios控件 UIControl

    < UIControl> 1 处理用户事件的控件的基类,如UIButton,UISlider等 2 一般不直接实例化,而是使用他的子类 3 可以通过跟踪触摸事件来设置和获取控件状态,并且这 ...