I am trying to map a Single<Response>
to a an Observable<Result<CustomObject, MoyaError>>
however I am unsure about how to do it.
My current function is as follows:
func accountInfo() -> Observable<AccountInfo> {
return provider
.rx
.request(.accountInfo)
.map(AccountInfo.self)
.asObservable()
}
However, I would like something as follows:
func accountInfo() -> Observable<Result<AccountInfo, MoyaError>> {
return provider
.rx
.request(.accountInfo)
.mapResult(AccountInfo.self)
.asObservable()
}
Any help creating a mapResult
extension would be great.
Thank you
Something like this:
extension Single {
var asObservableResult: Observable<Result<Element, Error>> {
return asObservable()
.map { .success($0) }
.catchError { .just(.failure($0)) }
}
}
Although it might make more sense to map it to a Driver rather than an Observable... Otherwise, you have two possible errors...
extension Single {
var asDriverResult: Driver<Result<Element, Error>> {
return asObservable()
.map { .success($0) }
.asDriver(onErrorRecover: { .just(.failure($0)) })
}
}