Observable which does not pass anything in onNext()

前端 未结 6 946
猫巷女王i
猫巷女王i 2021-02-02 06:52

I would need an Observable, for example to provide a system clock, which does not need to pass anything in onNext(). I couldn\'t find a signature that would allow me to do that.

6条回答
  •  迷失自我
    2021-02-02 07:12

    I don't know this will helps you or not.
    The code written in RxSwift.

    // Create an observable and emit somethings
    let myObservable = Observable.create{ observer in
        observer.on(.next(Void()))
        return Disposables.create()
    }
    
    // Observer subscribe changes
    myObservable.subscribe(onNext: {
        _ in
        print("Hello")
    }).disposed(by: disposeBag)
    

    Or use the Variable object

    // Create a Variable object that contanins nothing
    var myValueWatcher:Variable = Variable(Void())
    
    // Observer subscribe changes
    myValueWatcher.asObservable().skip(1).subscribe(onNext: {
        _ in
        print("Changes!")
    }).disposed(by: disposeBag)
    
    
    // The emit code
    myValueWatcher.value = Void()
    

提交回复
热议问题