1

I have a list of items say for example list of integers, if any of the integer is null, i need to consume the error and proceed with the next item

For example

Observable.fromIterable(listOf(1, 2, 3, null, 5))
                .map {
                    doSomeProcess(it)
                }
                .onErrorReturnItem(-1)
                .subscribeBy(
                        onNext = {
                           print(it)
                        },
                        onError = {

                        },
                        onComplete = {

                        })

I am expecting the output like this

1
2
3
-1
5

But my problem is after -1 it is not getting proceeded with item 5, it stops there, Can anyone help me out with this ?

Stack
  • 1,164
  • 1
  • 13
  • 26

1 Answers1

1

In Rx onError is a termination event. So if you want not to break a stream, but just handle error and keep receiving other data, you can use Notification.

 Observable.fromIterable(listOf(1, 2, 3, null, 5))
            .map {
                doSomeProcess(it)
            }
            .subscribeBy(
                onNext = {
                    when {
                        it.isOnNext -> {
                            val result = it.value
                            if(result == -1){
                                //handle error here
                            }
                        }
                        //it.isOnComplete -> {
                        //}
                        //it.isOnError -> {
                        //}
                    }
                }
            )


 private fun doSomeProcess(i: Int?): Notification<Int> =
    if (i == null)
        //Notification.createOnError(IllegalArgumentException())
        Notification.createOnNext(-1)
    else
        Notification.createOnNext(i)
Yamko
  • 535
  • 1
  • 3
  • 13