iOS之Swift框架理解RxSwiftiOS 开发

RxSwift_v1.0笔记——2 Observables

2017-07-16  本文已影响116人  大灰很

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
上一篇下一篇

猜你喜欢

热点阅读