监听事件序列
public enum Event<Element> {
/// Next element is produced.
case next(Element)
/// Sequence terminated with an error.
case error(Swift.Error)
/// Sequence completed successfully.
case completed
}
Observables Event
let first = 1
let second = 2
let observable = Observable<Int>.just(first)
let observable2 = Observable.of(first, second)
let observable3 = Observable.of([first, second])
let observable4 = Observable.from([first, second])
let ovservable5 = observable<Int>.never()
let observable6 = Observable<Int>.range(start: 1, count: 10)
observable2.subscribe {event in
print(event)
print(event.element)
}
// result: next(1) -> next(2) -> completed
// result: 1 2 nil
Observables Element
observable2.subscribe { element in
print(element)
} onError: { error in
print(error)
} onCompleted: {
print("onCompleted")
} onDisposed: {
print("onDisposed")
}
//or
observable2.subscribe { element in
print(element)
}
Disposing
Observable<String>.create { observer in
observer.onNext("1")
observer.onCompleted()
observer.onNext("?")
return Disposables.create()
}
// 1 -> Completed -> Disposed
Terminating
Observable<String>.create { observer in
observer.onNext("1")
observer.onError(MyError.anError)
observer.onNext("?")
return Disposables.create()
}