内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

RxSwift1.0笔记(二):Observables

2017-10-12 16:11 出处:清屏网 人气: 评论(0

observable的生命周期

这是一个简短的回顾:

  • observable发射包含元素的next事件。它能持续的发送直到:
  • 发射error事件被终止或
  • 发射completed事件被终止。
  • 一旦observable被终止,它将不再发送事件。

创建observables

just

just创建的sequence只能发出一种特定的事件,能正常结束.

of

of是创建一个sequence能发出很多种事件信号

from

from就是从集合中创建sequence,例如数组,字典或者Set

example(of: "just, of, from") {    
    // 1
    let one = 1
    let two = 2
    let three = 3

    // 2
    let observable: Observable<Int> = Observable<Int>.just(one)
    let observable2 = Observable.of(one, two, three)
    let observable3 = Observable.of([one, two, three])
    let observable4 = Observable.from([one, two, three])
}

订阅observables

subscribe

订阅sequence发出的事件,比如next事件,error事件等

example(of: "subscribe") {
    let one = 1
    let two = 2
    let three = 3
    let observable = Observable.of(one, two, three)
    observable.subscribe(onNext: { (element) in
        print(element)
    })
}
--- Example of: subscribe ---
1
2
3

empty

创建一个空的sequence,只能发出一个completed事件

example(of: "empty") { 
    let observable = Observable<Void>.empty()
    observable.subscribe(onNext: { (element) in
        print(element)
    }, onCompleted: {
        print("Completed")
    })
}
--- Example of: empty ---
Completed

never

never就是创建一个sequence,但是不发出任何事件信号

example(of: "never") {
    let observable = Observable<Any>.never()
    let disposeBag = DisposeBag()
    observable
        .do(onSubscribe: {
        print("Subscribed")
    })
        .subscribe(onNext: { (element) in
            print(element)
        }, onCompleted: { 
            print("Completed")
        }, onDisposed: { 
            print("Disposed")
        })
        .addDisposableTo(disposeBag)
}
--- Example of: never ---
Subscribed
Disposed

range

range就是创建一个sequence,他会发出这个范围中的从开始到结束的所有事件

example(of: "range") {
    // 1
    let observable = Observable<Int>.range(start: 1, count: 10)
    observable
        .subscribe(onNext: { i in
            // 2
            let n = Double(i)
            let fibonacci = Int(((pow(1.61803, n) - pow(0.61803, n)) /
                2.23606).rounded())
            print(fibonacci)
        })
}
--- Example of: range ---
0
1
2
3
5
8
13
21
34
55

Disposing and terminating

dispose

用来手动取消订阅

example(of: "dispose") {
    // 1
    let observable = Observable.of("A", "B", "C")
    // 2
    let subscription = observable.subscribe { event in
        // 3
        print(event)
    }
    subscription.dispose()
}
--- Example of: dispose ---
next(A)
next(B)
next(C)
completed

DisposeBag

其实就相当于iOS中的ARC,会在适当的时候销毁观察者,相当于内存管理者吧。

example(of: "DisposeBag") {
    // 1
    let disposeBag = DisposeBag()
    // 2
    Observable.of("A", "B", "C")
        .subscribe { // 3
            print($0)
        }
        .addDisposableTo(disposeBag) // 4
}
--- Example of: DisposeBag ---
next(A)
next(B)
next(C)
completed

create

可以自定义可观察的sequence

example(of: "create") { 
    enum MyError: Error {
        case anError
    }

    let disposeBag = DisposeBag()
    Observable<String>.create { observer in
        // 1
        observer.onNext("1")
//        observer.onError(MyError.anError)
        // 2
        observer.onCompleted()
        // 3
        observer.onNext("?")
        // 4
        return Disposables.create()
        }.subscribe(
            onNext: { print($0) },
            onError: { print($0) },
            onCompleted: { print("Completed") },
            onDisposed: { print("Disposed") }
        )
        .addDisposableTo(disposeBag)
}
--- Example of: create ---
1
Completed
Disposed

创建observable工厂

deferred

会为每一为订阅者observer创建一个新的可观察序列

example(of: "deferred") {
    let disposeBag = DisposeBag()
    // 1
    var flip = false
    // 2
    let factory: Observable<Int> = Observable.deferred {
        // 3
        flip = !flip
        // 4
        if flip {
            return Observable.of(1, 2, 3)
        } else {
            return Observable.of(4, 5, 6)
        }
    }
    for _ in 0...3 {
        factory.subscribe(onNext: {
            print($0, terminator: "")
        })
            .addDisposableTo(disposeBag)
        print()
    }
}
--- Example of: deferred ---
123
456
123
456
分享给小伙伴们:
本文标签: ObservablesRxSwift

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.

清屏网 版权所有 豫ICP备15026204号