RxJava 2
我有以下 2 个示例来说明如何使用skipUntil:
此函数将 lambda 函数传递给skipUntil:
sourceSubject
.skipUntil<Int> {
triggerSubject.subscribe(it)
}
.subscribeBy { println(it) }
这个通过了观察者:
sourceSubject
.skipUntil(triggerSubject)
.subscribeBy { println(it) }
但是,我对方法签名感到困惑,因为它似乎只需要一个观察者,而我看不到 lambda。只是想知道为什么我可以传递一个观察者并将 lambda 传递给这个函数
这是似乎只是显示传递观察者的方法:
public final <U> Observable<T> skipUntil(ObservableSource<U> other) {
ObjectHelper.requireNonNull(other, "other is null");
return RxJavaPlugins.onAssembly(new ObservableSkipUntil<T, U>(this, other));
}
提前非常感谢
最佳答案
此功能称为 SAM Conversions 。它基本上意味着
if an object implements a SAM interface (one with a Single Abstract Method), you can pass a lambda instead of creating and passing an object of the given interface.
在您的情况下,skipUntil
需要一个类型为ObservableSource的参数
由于 ObservableSource
是 SAM 接口(interface)
,因此您可以传递 lambda
而不是 类型的完整
.object
>ObservableSource
因此,即使没有需要 lambda
的 skipUntil
function
,您也可以传递一个,因为 SAM Conversions .
关于kotlin - 传递观察者并将 lambda 传递给skipUntil 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60468653/