1

Вот есть такой метод

public void updateDependedQuestionsIfNeeded(ServicePartFormQuestion mCurrentServicePartFormQuestion)
{
    try
    {
        if (mCurrentServicePartFormQuestion != null && mCurrentServicePartFormQuestion.getDependedQuestions() != null)
        {
            Observable.fromIterable(mCurrentServicePartFormQuestion.getDependedQuestions())//
                      .filter(//
                              iServicePartFormQuestion -> iServicePartFormQuestion != null //
                                      && iServicePartFormQuestion.getAnswerFieldTypeWithCorrection() == ServicePartFormQuestion.eAnswerFieldType.ClosedList//
                      )//
                      .map(iServicePartFormQuestion -> getVisibleQuestions().indexOf(iServicePartFormQuestion))//
                      .filter(iInteger -> iInteger > -1)//
                      .forEach(this::notifyItemDataChanged);
        }
    }
    catch (Exception ignored)
    {
    }
}

Как можно избавиться от это строчки проверки

if (mCurrentServicePartFormQuestion != null && mCurrentServicePartFormQuestion.getDependedQuestions() != null)

Я так понимаю, что сначала нужно как то проверить, что сам обьект не null , если нет, то сделать flatMap() и забрать из него getDependedQuestion() проверить если он тоже не null и если нет, то тогда уже работать с ним как описано в моем примере...

Только не пойму как сделать проверку на null

EDIT

Теперь ясно, что Rx не работает с null и нужно поставить onError() обработчик, но все равно не понятно куда его прицепить в моем случае

public void updateDependedQuestionsIfNeeded(ServicePartFormQuestion mCurrentServicePartFormQuestion)
{
    Observable.just(mCurrentServicePartFormQuestion)//
              .flatMap(//
                      iServicePartFormQuestion -> Observable.fromIterable(iServicePartFormQuestion.getDependedQuestions())//
              )//
              .cast(ServicePartFormQuestion.class)//
              .filter(//
                      iServicePartFormQuestion -> iServicePartFormQuestion != null //
                              && iServicePartFormQuestion.getAnswerFieldTypeWithCorrection() == ServicePartFormQuestion.eAnswerFieldType.ClosedList//
              )//
              .map(iServicePartFormQuestion -> getVisibleQuestions().indexOf(iServicePartFormQuestion))//
              .filter(iInteger -> iInteger > -1)//
              .forEach(this::notifyItemDataChanged);
}
4
  • 2
    rxjava2 не работает с null объектами, так что можно ничего не проверять, вам упадёт ошибка в onError :) 4 фев 2018 в 18:09
  • @SviatVolkov Добавил в вопрос то, что получилось. Все равно не пойму куда прицепить обработчик
    – Sirop4ik
    5 фев 2018 в 8:04
  • в методе subscribe() есть второй параметр. это Consumer в который будут падать ошибки 5 фев 2018 в 8:45
  • @SviatVolkov аа, все понял... Меня просто запутал метод forEach() после которого нет subscribe() метода. Теперь ясно
    – Sirop4ik
    5 фев 2018 в 9:08

1 ответ 1

0

В итоге получилось вот так

public void updateDependedQuestionsIfNeeded(ServicePartFormQuestion mCurrentServicePartFormQuestion)
{
    Observable.just(mCurrentServicePartFormQuestion)//
              .flatMap(//
                      iServicePartFormQuestion -> Observable.fromIterable(iServicePartFormQuestion.getDependedQuestions())//
              )//
              .cast(ServicePartFormQuestion.class)//
              .filter(//
                      iServicePartFormQuestion -> iServicePartFormQuestion != null //
                              && iServicePartFormQuestion.getAnswerFieldTypeWithCorrection() == ServicePartFormQuestion.eAnswerFieldType.ClosedList//
              )//
              .map(iServicePartFormQuestion -> getVisibleQuestions().indexOf(iServicePartFormQuestion))//
              .filter(iInteger -> iInteger > -1)//
              .subscribe(//
                      this::notifyItemDataChanged, //
                      iThrowable -> AppUtils.printLog(Log.ERROR, TAG, iThrowable.getMessage())//
              );
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.