RxJava Maybe:有什么巧妙的方法来处理 Empty case 吗?
RxJava Maybe: Any neat way to handle Empty case?
我卡在这个问题上了,应该很简单。我需要订户在 Maybe
作为 Empty Maybe
完成时执行代码块。我发现
- 我们可以传递默认的 Maybe 值或使用 switchIfEmpty,但我觉得两者都很糟糕。
- 还有一个订阅函数接受 onComplete 函数(以及其他两个事件的处理程序),但 onComplete 不接受任何可用于查找是否已完成为空的参数。
- 另一种方法可能是
Maybe.isEmpty.blockingGet()
,但它也很脏。
我试过以下(Kotlin 语法):-
fun <T> Maybe<T>.subscribeWithEmptyHandler(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit, onEmpty: () -> Unit) {
this.isEmpty.subscribe({ if (it) onEmpty() }, { onError(it) })
this.subscribe({ onSuccess(it) }, { onError(it) })
}
不过果然是运行订阅了两次,这里测试过:-
Maybe.create<Int> {
println("subscribing")
//Remove line below to create Empty Maybe
it.onSuccess(5)
it.onComplete()
}
.subscribeWithEmptyHandler({println("success")},{println("error")},{println("empty")})
有人可以建议更简洁的方法来解决这个问题吗?
我做了以下比我在问题中写的任何东西都整洁的东西:-
fun <T> Maybe<T>.subscribeWithEmptyHandler(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit, onEmpty: () -> Unit) {
this.toSingle()
.subscribe(
{ onSuccess(it) },
{ if (it is NoSuchElementException) onEmpty() else onError(it) }
)
}
这里只订阅一次,不涉及创建新的默认值。仍然不确定这是否是最好的方法。
使用Maybe.doOnEvent
(java示例):
Maybe
.empty()
.doOnEvent((value, error)-> {
if (value==null && error == null) {
System.out.println("empty!");
}})
.subscribe();
有一个使用flatMap的解决方案
return Maybe.just<String>(smth)
.flatMap(
Function {
Maybe.just(it) // onSuccess
},
Function {
Maybe.error(it) // onError
},
Callable { // onComplete
Maybe.just("Empty")
}
)
或者
return Maybe.just<String>(smth)
.flatMap(
{
Maybe.just<String>(it) // onSuccess
},
{
Maybe.error(it) // onError
},
{
Maybe.just("Empty") // onComplete
}
)
我卡在这个问题上了,应该很简单。我需要订户在 Maybe
作为 Empty Maybe
完成时执行代码块。我发现
- 我们可以传递默认的 Maybe 值或使用 switchIfEmpty,但我觉得两者都很糟糕。
- 还有一个订阅函数接受 onComplete 函数(以及其他两个事件的处理程序),但 onComplete 不接受任何可用于查找是否已完成为空的参数。
- 另一种方法可能是
Maybe.isEmpty.blockingGet()
,但它也很脏。
我试过以下(Kotlin 语法):-
fun <T> Maybe<T>.subscribeWithEmptyHandler(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit, onEmpty: () -> Unit) {
this.isEmpty.subscribe({ if (it) onEmpty() }, { onError(it) })
this.subscribe({ onSuccess(it) }, { onError(it) })
}
不过果然是运行订阅了两次,这里测试过:-
Maybe.create<Int> {
println("subscribing")
//Remove line below to create Empty Maybe
it.onSuccess(5)
it.onComplete()
}
.subscribeWithEmptyHandler({println("success")},{println("error")},{println("empty")})
有人可以建议更简洁的方法来解决这个问题吗?
我做了以下比我在问题中写的任何东西都整洁的东西:-
fun <T> Maybe<T>.subscribeWithEmptyHandler(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit, onEmpty: () -> Unit) {
this.toSingle()
.subscribe(
{ onSuccess(it) },
{ if (it is NoSuchElementException) onEmpty() else onError(it) }
)
}
这里只订阅一次,不涉及创建新的默认值。仍然不确定这是否是最好的方法。
使用Maybe.doOnEvent
(java示例):
Maybe
.empty()
.doOnEvent((value, error)-> {
if (value==null && error == null) {
System.out.println("empty!");
}})
.subscribe();
有一个使用flatMap的解决方案
return Maybe.just<String>(smth)
.flatMap(
Function {
Maybe.just(it) // onSuccess
},
Function {
Maybe.error(it) // onError
},
Callable { // onComplete
Maybe.just("Empty")
}
)
或者
return Maybe.just<String>(smth)
.flatMap(
{
Maybe.just<String>(it) // onSuccess
},
{
Maybe.error(it) // onError
},
{
Maybe.just("Empty") // onComplete
}
)