英文:
How to add andthen() operator under condition or inside a loop Rx Kotlin
问题
I'm trying to chain some rx operations under a condition but the andThen() inside the apply does not execute. Is this correct? operation returns a Completable. Android studio gives me the warning "The result of andThen is not used" in both andthen() calls.
尝试在某个条件下链接一些Rx操作,但是apply内部的andThen()不执行。这样写正确吗?operation返回一个Completable。在两个andthen()调用中,Android Studio给我一个警告:"andThen的结果未被使用"。
Same happens here, only doAnOperation(aList[0]) is executed, should I add a .subscribe inside the andThen()?
在这里也是同样的情况,只有doAnOperation(aList[0])被执行,我应该在andThen()内部添加.subscribe()吗?
英文:
I'm trying to chain some rx operations under a condition but the andthen() inside the apply does not execute. Is this correct? operation returns a Completable. Android studio gives me the warning "The result of andThen is not used" in both andthen() calls.
operation.apply {
if (true) {
andthen(thisDoesNotExecute())
}
}
.andThen(thisExecutes())
Same happens here, only doAnOperation(aList[0]) is executed, should I add a .subscribe inside the andThen()?
operation(aList[0])
.apply {
if (aList.size > 1) {
for (item in aList.sublist(1, aList.size)) {
andThen(operation(item))
}
}
}
.subscribe()
答案1
得分: 2
andThen
返回一个包含你的转换的新实例,它不会改变原始源对象,所以如果你想要考虑它,你必须使用它的返回值。
apply()
返回你调用它的实例,所以在你的情况下它将返回最初的 operation
,而不是 andThen()
转换的结果。
你应该阅读一下作用域函数,以了解哪一个最适合你。在这里,你想要返回 andThen()
的结果,所以你需要一个返回 lambda 结果而不是接收者的作用域函数。然后,你可以选择将 operation
对象作为 this
或 it
放在 lambda 内部,从而决定使用 run
还是 let
。
让我们在这里使用 let
:
operation.let {
if (condition) {
it.andthen(thisDoesNotExecute())
} else {
it
}
}
.andThen(thisExecutes())
如果你经常需要这个,你可以创建自己的扩展函数来封装它。
当然,你也可以使用中间变量:
val intermediate = if (condition) {
operation.andThen(thisDoesNotExecute())
} else {
operation
}
val result = intermediate.andThen(thisExecutes())
对于你的循环,你可以像这样使用 fold()
:
aList.fold(Completable.complete()) { acc, item -> acc.andThen(operation(item)) }
.subscribe()
或者为了保持与你最初的方法尽可能接近,而不使用初始的 complete()
元素,你可以首先使用 map
,然后使用 reduce()
:
aList.map { operation(it) }
.reduce { c1, c2 -> c1.andThen(c2) }
.subscribe()
英文:
andThen
returns a new instance with your transformation, it doesn't mutate the original source, so you have to use its return value if you want to take it into account.
apply()
returns the instance on which you call it, so in your case it will return the initial operation
, not the result of the andThen()
transformation.
You should read up on scope functions to understand which one suits you best. Here you want to return the result of andThen()
, so you need a scope function that returns the lambda result, not the receiver. Then you can choose whether you want to have the operation
object as this
or it
inside the lambda, which decides between run
or let
.
Let's go with let
here:
operation.let {
if (condition) {
it.andthen(thisDoesNotExecute())
} else {
it
}
}
.andThen(thisExecutes())
If you need this often, you could create your own extension function to encapsulate that.
You could also use an intermediate variable instead of course:
val intermediate = if (condition) {
operation.andThen(thisDoesNotExecute())
} else {
operation
}
val result = intermediate.andThen(thisExecutes())
For your loop, you could use fold()
like this:
aList.fold(Completable.complete()) { acc, item -> acc.andThen(operation(item)) }
.subscribe()
Or to stay really close to your initial approach without initial complete()
element, you could map
first and then use reduce()
:
aList.map { operation(it) }
.reduce { c1, c2 -> c1.andThen(c2) }
.subscribe()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论