美文网首页
RxSwift笔记:Observables

RxSwift笔记:Observables

作者: 渐行渐远ty | 来源:发表于2022-05-24 16:23 被阅读0次

监听事件序列

事件序列
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()
}

相关文章

网友评论

      本文标题:RxSwift笔记:Observables

      本文链接:https://www.haomeiwen.com/subject/kgfdprtx.html