RxJava Различные операции для одной подписки и ожидания, пока все операции не будут завершены - PullRequest
0 голосов
/ 16 апреля 2019

У меня есть куча запросов, которые я должен сделать в базе данных, выполняя некоторые операции с плоской картой. В методе подписки мне нужно сделать какой-то новый элемент foreach для запроса (используя цикл for). После этого я хочу обработать условие, но проблема в том, что результат rxjava еще не готов, что нарушает мою логику.

Как ждать, пока все описанные ниже операции не будут завершены, и затем обработать мою логику? Пожалуйста, следуйте двум комментариям.

Это мой код:

private void startBundlePackItemSelection(int bundlePackQuantity) {
    if (recipeNo != null && !recipeNo.isEmpty())
        compositeDisposable.add(bundlePackDialogInteract.selectAllWithSameRecipeNo(recipeNo).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.single())
                .flatMap(Observable::fromIterable)
                .flatMap(bundlePackRecipeTrigger -> bundlePackDialogInteract.findRecipeTriggerLines(recipeNo, bundlePackRecipeTrigger.getId()))
                .flatMap(Observable::fromIterable)
                .subscribe(bundlePackTriggerInnerCondition -> {
                    if (bundlePackTriggerInnerCondition.getColumName().equalsIgnoreCase("ItemCode")) {
                        //I want to wait line below until all this one is over ,which is also a RxJavaOperation
                        checkIfCanInsertItemToDatabase(bundlePackQuantity, bundlePackTriggerInnerCondition);
                    } else {
                        bundlePackTriggerInnerConditionsHelper.add(bundlePackTriggerInnerCondition);
                    }
                    if (bundlePackTriggerInnerConditionsHelper.size() > 0 && canContinueToBundlePackFragment) {
                        //This is where I get stuck because my boolean is still false , for the reason that the checkIfCanInsertItemToDatabase is not finished yet. 
                    }
                }, Throwable::printStackTrace)
        );
}

private void checkIfCanInsertItemToDatabase(int bundlePackQuantity, BundlePackTriggerInnerCondition lineToInsert) {
    final double[] quantityHelper = {-1};
    compositeDisposable.add(bundlePackDialogInteract.findItem(lineToInsert.getValue()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.single())
            .flatMap(item -> bundlePackDialogInteract.findItemStock(item.getCode(), FilterArgs.getWarehouseCode()))
            .flatMap(itemStock -> {
                quantityHelper[0] = itemStock;
                return bundlePackDialogInteract.findQuantityFromChild(lineToInsert.getReferenceId(), lineToInsert.getRecipeNo());
            }).flatMap(currentRowQuantity -> {
                if ((bundlePackQuantity * currentRowQuantity) <= quantityHelper[0]) {
                    canContinueToBundlePackFragment = true;
                    return bundlePackDialogInteract.findItem(lineToInsert.getValue());
                } else {
                    addBundlePackDialogView.bundlePackNotAvailableForYou();
                    canContinueToBundlePackFragment = false;
                    return Observable.just(null);
                }
            })
            .subscribe(item -> {
                Log.d("BundlePackFeature", " ITEM READY TO INSERT");
                canContinueToBundlePackFragment = true;
            }, Throwable::printStackTrace));
}

1 Ответ

0 голосов
/ 16 апреля 2019

Если вы хотите подождать, пока checkIfCanInsertItemToDatabase не получит результат, вам, вероятно, следует заставить эту функцию возвращать Single так, чтобы вы могли вызывать flatmap для нее.Знайте, как работает Observable.just(T) (как уже упоминалось здесь ).

РЕДАКТИРОВАТЬ Попробуйте что-то вроде этого

private void startBundlePackItemSelection(int bundlePackQuantity) {
    if (recipeNo != null && !recipeNo.isEmpty())
        compositeDisposable.add(bundlePackDialogInteract.selectAllWithSameRecipeNo(recipeNo).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.single())
                .flatMap(Observable::fromIterable)
                .flatMap(bundlePackRecipeTrigger -> bundlePackDialogInteract.findRecipeTriggerLines(recipeNo, bundlePackRecipeTrigger.getId()))
                .flatMap(Observable::fromIterable)
                .flatMap(checkIfCanInsertItemToDatabase(int bundlePackQuantity, BundlePackTriggerInnerCondition lineToInsert))
                .subscribe(canBeInsertedToDB -> {
                    // Do something with your result
                }, Throwable::printStackTrace)
        );
}

private void checkIfCanInsertItemToDatabase(int bundlePackQuantity, BundlePackTriggerInnerCondition lineToInsert) : Single<Boolean>{
    final double[] quantityHelper = {-1};
    return  Single.create<Boolean> { singleSubscriber ->
        bundlePackDialogInteract.findItem(lineToInsert.getValue()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.single())
            .flatMap(item -> bundlePackDialogInteract.findItemStock(item.getCode(), FilterArgs.getWarehouseCode()))
            .flatMap(itemStock -> {
                quantityHelper[0] = itemStock;
                return bundlePackDialogInteract.findQuantityFromChild(lineToInsert.getReferenceId(), lineToInsert.getRecipeNo());
            }).flatMap(currentRowQuantity -> {
                if ((bundlePackQuantity * currentRowQuantity) <= quantityHelper[0]) {
                    singleSubscriber.onNext(true)
                } else {
                    addBundlePackDialogView.bundlePackNotAvailableForYou();
                    singleSubscriber.onNext(false)
                }
            })

    }
}
...