我正在寻找解决方案来衡量某些任务的执行情况
我想这与使用 EventBus 发布开始计数和停止事件非常相似。
所以我可以有一个功能:
doSomething(){
//start counting
}
在其他函数中,我想访问计数器并在其上调用 stop:
otherFunctionWhichCanBeInvokedLater(){
//stop counter and get the duration time
}
有没有很好的方法在 Rx 中完成这个?
我想为此使用 Kotlin 和 RxJava2。
最佳答案
使用 doOnXXX
怎么样?钩子(Hook),像这样:
fun startTimer() {
//start counting
}
fun stopTimer() {
//stop counter and get the duration time
}
fun observe() {
Observable.just(...)
.doOnSubscribe { startTimer() }
.doFinally { stopTimer() }
.subscribeBy(...)
}
根据您的要求,您可以替换
doOnFinally
使用 doOnComplete
的设置和/或 doOnError
.
关于kotlin - 如何计算 observable 的执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212436/