创建 和 订阅 Observables(可观察者)
(1) never
Creates a sequence that never terminates and never emits any events.
创建一个序列,永远不会终止,不发出任何事件。
example("never") {
let disposeBag = DisposeBag()
let neverSequence = Observable<String>.never()
let neverSequenceSubscription = neverSequence
.subscribe { _ in
print("This will never be printed")
}
neverSequenceSubscription.disposed(by: disposeBag)
}
(2)empty
Creates an empty Observable sequence that only emits a Completed event.
创建一个空的可观察序列,只发出一个已完成的事件。
example("empty") {
let disposeBag = DisposeBag()
Observable<Int>.empty()
.subscribe { event in
print(event)
}
.disposed(by: disposeBag)
}
(3)just
Creates an Observable sequence with a single element
创建一个 单一元素 的可观察序列
example("just") {
let disposeBag = DisposeBag()
Observable.just("🔴")
.subscribe { event in
print(event)
}
.disposed(by: disposeBag)
}
(4)of
Creates an Observable sequence with a fixed number of elements.
创建一个具有固定数目的元素的可观察序列。
example("of") {
let disposeBag = DisposeBag()
Observable.of("🐶", "🐱", "🐭", "🐹")
.subscribe(onNext: { element in
print(element)
})
.disposed(by: disposeBag)
}
本例介绍了使用subscribe(OnNext:)简便方法。
与subscribe不同的是:subscribe发送了所有的事件(next 值,error 错误 ,completed 完成);
subscribe(onNext:) 忽略了error 和 completed事件,仅发送了next 事件;
subscribe(OnNext:)也有订阅成功和完成的方法,如下例
someObservable.subscribe(
onNext: { print("Element:", $0) },
onError: { print("Error:", $0) },
onCompleted: { print("Completed") },
onDisposed: { print("Disposed") }
)
(5)from
Creates an Observable sequence from a Sequence, such as an Array, Dictionary, or Set.
从 一个序列 中创建可观察序列 例如:数组、字典 、集合
example("from") {
let disposeBag = DisposeBag()
Observable.from(["🐶", "🐱", "🐭", "🐹"])
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
这个示例还演示了使用默认参数名称$ 0而不是显式命名参数。
(6)creat
Creates a custom Observable sequence
创建一个自定义的可观察序列
example("create") {
let disposeBag = DisposeBag()
let myJust = { (element: String) -> Observable<String> in
return Observable.create { observer in
observer.on(.next(element))
observer.on(.completed)
return Disposables.create()
}
}
myJust("🔴")
.subscribe { print($0) }
.disposed(by: disposeBag)
}
(7)range
Creates an Observable sequence that emits a range of sequential integers and then terminates
创建一个可观察序列发出一系列连续的整数,然后终止。
example("range") {
let disposeBag = DisposeBag()
Observable.range(start: 1, count: 10)
.subscribe { print($0) }
.disposed(by: disposeBag)
}
(8)repeatElement
Creates an Observable sequence that emits the given element indefinitely
创建一个sequence,发出特定的事件n次
example("repeatElement") {
let disposeBag = DisposeBag()
Observable.repeatElement("🔴")
.take(3)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
本例使用 take 操作符 从序列的开始 返回指定数目的元素。
(9)generate
Creates an Observable sequence that generates values for as long as the provided condition evaluates to true.
generate是创建一个可观察sequence,当初始化的条件为true的时候,他就会发出所对应的事件
example("generate") {
let disposeBag = DisposeBag()
Observable.generate(
initialState: 0,
condition: { $0 < 3 },
iterate: { $0 + 1 }
)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
(10)deferred
deferred会为每一为订阅者observer创建一个新的可观察序列
example("deferred") {
let disposeBag = DisposeBag()
var count = 1
let deferredSequence = Observable<String>.deferred {
print("Creating \(count)")
count += 1
return Observable.create { observer in
print("Emitting...")
observer.onNext("🐶")
observer.onNext("🐱")
observer.onNext("🐵")
return Disposables.create()
}
}
deferredSequence
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
deferredSequence
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
例子中每次进行subscribe的时候都会去创建一个新的deferredSequence,所以Emitting会打印两遍。
(11)error
Creates an Observable sequence that emits no items and immediately terminates with an error.
创建一个可观察序列,但不发出任何正常的事件,只发出error事件并结束
example("error") {
let disposeBag = DisposeBag()
Observable<Int>.error(TestError.test)
.subscribe { print($0) }
.disposed(by: disposeBag)
}
(12)doOn
Invokes a side-effect action for each emitted event and returns (passes through) the original event.
doOn我感觉就是在直接onNext处理时候,先执行某个方法,doOnNext( :)方法就是在subscribe(onNext:)前调用,doOnCompleted(:)就是在subscribe(onCompleted:)前面调用的。
example("doOn") {
let disposeBag = DisposeBag()
Observable.of("🍎", "🍐", "🍊", "🍋")
.do(onNext: { print("Intercepted:", $0) }, onError: { print("Intercepted error:", $0) }, onCompleted: { print("Completed") })
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
There are also doOnNext(:), doOnError(:), and doOnCompleted(_:) convenience methods to intercept those specific events, and doOn(onNext:onError:onCompleted:) to intercept one or more events in a single call.
网友评论