订阅的Thoulable onError组件未按预期编译

时间:2017-01-12 23:08:23

标签: java rx-android throwable rxandroidble

尝试使用Observable设置对RxAndroidBle连接状态的监视。下面的代码编译(我还无法测试它),但我不完全理解为什么。 subscribe调用的第二个参数应为Action1<java.lang.Throwable> onError。我是否正确实现了这一点?为什么我不能写

throwable -> throw throwable

当我尝试时,第二个“throwable”被标记为“无法解析符号'throwable'”,以及“ - &gt;”之间并且“抛出”它说它期待右边的撑杆,左撑杆或分号。

// set up monitoring of connection state with a subscription
boolean setConnectionStateNotification() {
    asBleDevice.observeConnectionStateChanges()
        .subscribe(
            connectionState -> asBleConnectionState = connectionState,
            throwable -> new RuntimeException( "Problem with subscription to Connection State Changes: "
                            + throwable.getMessage() )
            );
    return true;
}

TBH我无法理解Action1<Throwable>的概念;有人可以解释一下吗?

更新:我想我可能已经弄明白了。像这样:

 boolean setConnectionStateNotification() {
    asBleDevice.observeConnectionStateChanges() // returns Observable<RxBleConnection.RxBleConnectionState>
        .subscribe(
            connectionState -> asBleConnectionState = connectionState,
            throwable -> { throw new RuntimeException(
                "Problem with subscription to Connection State Changes: "
                    + throwable.getMessage(), throwable );
            },
            ( ) -> RxBleLog.d( "Connection State Observable has completed", null ) // onCompleted() with no arguments
            ); // subscribe
    return true;
}

(我还为onCompleted()调用添加了第三个可选处理程序。)

1 个答案:

答案 0 :(得分:0)

我认为这可能是对的:

boolean setConnectionStateNotification() {
    asBleDevice.observeConnectionStateChanges() // returns Observable<RxBleConnection.RxBleConnectionState>
        .subscribe(
            connectionState -> asBleConnectionState = connectionState, // save value
            throwable -> { throw new RuntimeException(
                "Problem with subscription to Connection State Changes: "
                    + throwable.getMessage(), throwable );
            },
            ( ) -> RxBleLog.d( "Connection State Observable has completed", null ) // onCompleted() with no arguments
        ); // subscribe
    return true;
}