//: Please build the scheme 'RxSwiftPlayground' first
import XCPlayground
import RxSwift
func exampleOf(description: String, action: (Void) -> Void) {
print("\n--- Example of:", description, "---")
action()
}
enum XPError: Error {
case common
}
// 3种结束事件流的情况:
//
// 1.observer.onError(XPError.common)
// 2.observer.onCompleted()
// 3.subscriber.dispose()
var observable = Observable<Any>.create { observer -> Disposable in
observer.onNext("hello")
observer.onNext(1)
//observer.onError(XPError.common) // 1
//observer.onCompleted() // 2
return Disposables.create(){
print("Observable dispose")
}
}
let subscriber = observable.subscribe(onNext: { element in
print("onNext \(element)")
}, onError: { error in
print(error)
}, onCompleted: {
print("complete !")
}) {
print("subscriber dispose")
}
//subscriber.dispose() //3