How to check observable on non null



  • api.getActiveSimpleOrder(rest_id) 
                        .subscribeOn(Schedulers.io())
                      ****  .flatMap( result-> { if(result!=null) Observable::from;}) ******
                        .flatMap(offer -> api.getStatus(offer.getId()) // запрашиваем статус
                                .doOnNext(offer::setStatus) // записываем полученный статус в объект заказа
                                .map(status -> offer) // небольшая хитрость что бы дальше шли объекты заказов а не статусов
                        )
                        .toList() // преобразуем последовательность обратно в список
                        .observeOn(AndroidSchedulers.mainThread())// дальнейшие действия над данными будем производить в главном потоке
                        .subscribe(offers::addAll,
                                Exception -> Toast.makeText(
                                        getApplicationContext(), (CharSequence) Exception, Toast.LENGTH_LONG).
                                        show(),
                                () -> {
                                    Toast.makeText(
                                            getApplicationContext(), "Data Receive Completed", Toast.LENGTH_SHORT).
                                            show();
                                    sortOffers(offers);
                                });
    

    api.getActiveSimpleOrder(rest_id) returns the List, which may be null or void, if it's not empty, it works, but if the empty line ****** **** returns the expedition that doesn't even catch, can be from asynchronity,

    The question is, how do you tell the algorithm that if the observatory comes to null, nothing in the code needs to be done?



  • This is not a complete answer to the question, but solves the problem of erroneous processing of this piece of code,

    When the empty sheet (null) is received, the OnError method is called in Observable and the programme is being implemented further, the error was in the OnError method, namely in the line.

    Toast.makeText(getApplicationContext(), (CharSequence) Exception, Toast.LENGTH_LONG).show(),
    

    That is (CharSequence) Exception, where the name of the class was and the name of the variable, so it was so hard to see and catch, because it all pointed to OnError, but it wasn't Observable, it was in the body of the method.

    It should also be added that if the Observable is the nullity of the OnComplete method immediately. It should also be noted that there is an Onservable.empty() type of data that does not produce anything and that it does. That's what I got.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2