https://kotlinlang.org logo
Title
r

reline

07/15/2019, 5:25 PM
hey guys, I’d like some help figuring out how to better handle errors emitted from this observable. I want to parse multiple files eagerly, then wait for all of them to complete, and know which files failed. Is this possible? This is what I have so far:
fun parseObservable(f: File): Observable<Item> { ... }

Observable.fromIterable(List<File>)
                .concatMapEagerDelayError({ parseObservable(it) }, true)
                .toList()
                .subscribeOn(<http://Schedulers.io|Schedulers.io>())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { items -> ... }
r

rook

07/15/2019, 10:21 PM
You could catch the failure and wrap it. e.g.
sealed class Result(){
  class Content(val content: Type) : Result
  class Error(val error: Throwable): Result
}
Then you have a list of
Result
and you can decide what to do based on the type of
Result
it is
👍 1
u

ursus

07/16/2019, 10:49 AM
If you want to wait for all to complete/error, not the first one, then you dont want rx error at all, since that is like a exception, it exits your normal flow. So, yes what above is said, map it into some error constant / wrap into Result type. Then use Single.zip for example
r

reline

07/19/2019, 6:16 PM
Thanks again guys! Here’s what I ended up with:
fun parseObservable(f: File): Observable<Result> {
    ...
    .map { Success(f, it) }
    .onErrorResumeNext { Single.just(Failure(f, it)) }
    .toObservable()
}

Observable.fromIterable(List<File>)
                .concatMapEager { parseObservable(it) }
                .toList()
                .subscribe { results -> ... }
👍 1
u

ursus

07/19/2019, 7:50 PM
@reline onErrorReturn { t -> Failure(...) }
🙏 1