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 a List which may be null Or not, if it is not empty then everything works. but if empty, then the string *** **** returns an expression that is not caught even by using try catch, maybe because of asynchrony,

The question is: how can the algorithm make it clear that if the observant arrives at null, then nothing needs to be done from what is written in the code?

  • and filter( r -> r != null ) doesn't suit you? - zRrr

1 answer 1

This is not a complete answer to the question, but solves the problem with an error when working on this piece of code,

When a blank sheet (null) is received in Observable, the OnError method is called, and the program is executed further, the error was in the body of the OnError method namely in the string

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

namely, (CharSequence) Exception, where the class name was also the name of a variable, so it was so hard to see and catch because everything pointed to OnError, but it was not Observable but in the body of the method.

It is also worth adding that in case the incoming Observable - null> calls the OnComplete method immediately. It is also worth noting that there is an Onservable.empty () data type which does not generate anything and completes its execution. I have it all