How to stop and resume Observable.interval emiting ticks

后端 未结 7 2010
野性不改
野性不改 2020-12-13 06:32

This will emit a tick every 5 seconds.

Observable.interval(5, TimeUnit.SECONDS, Schedulers.io())
            .subscribe(tick -> Log.d(TAG, "tick = &qu         


        
7条回答
  •  囚心锁ツ
    2020-12-13 07:27

    You can use takeWhile and loop until conditions is true

    Observable.interval(1, TimeUnit.SECONDS)
            .takeWhile {
                Log.i(TAG, " time " + it)
                it != 30L
            }
            .subscribe(object : Observer {
                override fun onComplete() {
                    Log.i(TAG, "onComplete " + format.format(System.currentTimeMillis()))
                }
    
                override fun onSubscribe(d: Disposable) {
                    Log.i(TAG, "onSubscribe " + format.format(System.currentTimeMillis()))
                }
    
                override fun onNext(t: Long) {
                    Log.i(TAG, "onNext " + format.format(System.currentTimeMillis()))
                }
    
                override fun onError(e: Throwable) {
                    Log.i(TAG, "onError")
                    e.printStackTrace()
                }
    
            });
    

提交回复
热议问题