RxJava: upstream never completes when error is swallowed

后端 未结 2 518
谎友^
谎友^ 2021-01-16 07:28

I\'m using RxJava to iterate over a list of files, making a network call to upload each file, then collect the files that were successfully uploaded in a list and persist th

2条回答
  •  死守一世寂寞
    2021-01-16 07:46

    Your problem is that Single can only result in two values, a successful result or a failure. Turning the failure in an 'ignored' state can be done by first converting it to a Maybe and then using essentially the same code to handle failure and success.

    Maybe.onErrorResumeNext with a return value of Maybe.empty() would result in 0 or 1 results while Maybe.map only executes if it has a value, accurately handling the problem as you've described it.

    Adapted code:

            .flatMapMaybe { file ->
                uploadFile(file).toMaybe()
                        .onErrorResumeNext { error: Throwable ->
                            log(error)
                            Maybe.empty()
                        }
                        .map { response ->
                            file.id = response.id
                            file
                        }
            }
    

提交回复
热议问题