'why I can't keep observable alive after .onError() even I already use error handling like .catchError() onRxSwift?

So, I want to keep the observable keep alive (not disposhed) even after input onError(), so I can input onNext()

I already follow this answer

by adding .catchError() or .catchErrorReturn() but it didn't work like I expected

(nb: I run this code on playground)

    let bag = DisposeBag()
    let data = PublishSubject<Observable<Int>>()
    data.debug("debug").flatMap({
        $0.map({data in
            return data
        }).catchError({err in
            return Observable.empty()
        })
    }).subscribe(onNext:{
        print($0)
    },onCompleted:{
        print("end")
    }).disposed(by: bag)
    let inputData = PublishSubject<Int>()
    data.onNext(inputData)
    inputData.onNext(1)
    inputData.onNext(2)
    inputData.onError(MyError.anError)
    inputData.onNext(2)

actual result I get

1
2

expected result I want

1
2
2


Solution 1:[1]

For the best solution, I would suggest using materialize() with compactMap. take a look at https://michaellong.medium.com/rxswift-better-error-handling-with-compactmap-48a5d314d0f1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 evya