У меня есть куча запросов, которые я должен сделать в базе данных, выполняя некоторые операции с плоской картой. В методе подписки мне нужно сделать какой-то новый элемент 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));
}