RxJava:拆分Rx可流入多个流

时间:2017-05-04 19:52:15

标签: java stream rx-java

我想在流上执行一些操作,然后将流分成两个流,然后分别处理它们。

显示问题的示例:

class SuccessfulObject {
    private boolean success;
    private int id;

    public SuccessfulObject(boolean success, int id) {
        this.success = success;
        this.id = id;
    }

    public boolean isSuccess() {
        return success;
    }
    public boolean isFail() {
        return !success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    @Override
    public String toString() {
        return "SuccessfulObject{" +
                "id=" + id +
                '}';
    }
}

类别:

$('#proposal-type').text("Type of Project *");
$('.drop-item-input').on('change', function () {
    var proposalVal = "";
    var proposalHtml = "";
    $('.drop-item-input').each(function () {
        if ($(this).is(":checked")) {
            proposalVal += $(this).val();
            proposalHtml += '<div class="drop-item-selected"><span class="drop-item-close" data-attr=' + $(this).val() + '></span>' + $(this).val() + '</div>';
            $(this).parent().hide();
            //$(this).closest('label').fadeOut();
        };
        //else if ($(this).is(":checked") === false) {
        //  $(this).closest('label').fadeIn();
        //};
        $('#proposal-type').val(proposalVal).html(proposalHtml);
        $('#proposal-type-drop').removeClass('active');

        bindCloseEvent();
    });
    //values
    var type = $('.drop-item-input:checked').map(function () {
        return $(this).val();
    }).get().join(', ');
    console.log(type);
});

function bindCloseEvent() {
    $('.drop-item-close').click(function () {
        debugger;
        var selectedOption = $(this).attr('data-attr');
        var $handler = $('#proposal-type-drop').find('input[value="' + selectedOption + '"]');
        $handler.parent().show();
        $(this).parent().remove();

        if ($('#proposal-type').text() == "")
            $('#proposal-type').text("Type of Project *");

        //$(this).fadeOut("medium", function () {
        //    $(this).detach().appendTo('#proposal-type-drop').fadeIn();
        //});
        //$(this).is(":checked") === false;
        //$(this).closest('label').fadeIn();
        //$(this).closest('.drop-item-selected').fadeOut();
    });
}

但是这段代码会打印所有元素两次,而我想在分割一次之前执行所有操作。

输出:

  

OnNextNotification [SuccessfulObject {ID = 0}]
  OnNextNotification [SuccessfulObject {ID = 1}]
  OnNextNotification [SuccessfulObject {id = 2}]
OnCompleteNotification
  OnNextNotification [SuccessfulObject {ID = 0}]
  OnNextNotification [SuccessfulObject {ID = 1}]
  OnNextNotification [SuccessfulObject {id = 2}]
OnCompleteNotification

如何处理流以接收此行为?

1 个答案:

答案 0 :(得分:4)

使用publish分享对来源的订阅:

Flowable<Integer> source = Flowable.range(1, 5);

ConnectableFlowable<Integer> cf = source.publish();

cf.filter(v -> v % 2 == 0).subscribe(v -> System.out.println("Even: " + v));

cf.filter(v -> v % 2 != 0).subscribe(v -> System.out.println("Odd: " + v));

cf.connect();