RxJava PublishSubject订阅没有任何作用。订阅有什么用吗?

时间:2020-07-09 14:43:12

标签: java android multithreading rx-java rx-java3

学习RxJava,因此希望获得任何建议。

计算准备就绪时需要接收对象,因此我在模型方法中设置了PublishSubject

public PublishSubject<BaseUnit> exec(int inputNumber) {

    if (unitList.size() > 0) {
        for (BaseUnit unit : unitList) {
            unit.setInProgress();
        }
    }

    PublishSubject<BaseUnit> subject = PublishSubject.create();

    list = new ArrayList<>();

    populateList(inputNumber).
            subscribeOn(Schedulers.from(Executors.newFixedThreadPool(ThreadPool.getPoolSize())))
            .subscribe(calculatedList -> {
                list = calculatedList;

                for (List<Integer> elem : list) {
                    for (ListOperationName operationName : ListOperationName.values()) {
                        ListUnit unit = new ListUnit(operationName, elem, 0);
                        calculate(unit);
                        unitList.add(unit);
                        subject.onNext(unit);
                    }
                }

            }, error -> Log.d("ERROR", error.toString()));

    return subject;
}


public Observable<ArrayList<List<Integer>>> populateList(int inputNumber) {
    return Observable.fromCallable(() -> {

        ArrayList<List<Integer>> list = new ArrayList<>();

        Integer[] populatedArray = new Integer[inputNumber];
        Arrays.fill(populatedArray, insertValue);

        list.add(new ArrayList<>(Arrays.asList(populatedArray)));
        list.add(new LinkedList<>(Arrays.asList(populatedArray)));
        list.add(new CopyOnWriteArrayList<>(Arrays.asList(populatedArray)));

        return list;
    });
}

然后尝试订阅主持人:

public void calculate(int inputNumber) {

    fragment.showAllProgressBars();

    repository.exec(inputNumber)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.from(Executors.newFixedThreadPool(ThreadPool.getPoolSize())))
            .subscribe(unit -> {
                Log.d("PRESENTER RESULT", unit.toString());
                fragment.setCellText(unit.getViewId(), unit.getTimeString());

            }, error -> Log.d("PRESENTER ERROR", error.toString()));
}

这什么也没给我。但是,如果我使用ReplaySubject-它会给我所有结果,但似乎只使用一个线程。因此,我认为订阅时出现了一些错误,应该早一些。 我需要使用PublishSubject来给我提供结果,因为它们已经准备好使用多个线程。

该如何解决?也许还有其他问题?

0 个答案:

没有答案