如何结合观察两个结果 rx 序列并订阅一个闭包参数?

How do I combine observe two result rx sequences and subscribe with one closure arguments?

我的示例是创建呼叫休息 api 来自 moya

的负载 rx.request api 的两个可观察结果
func dataOneObservable() -> Observable<ObJectOneClass> {
        
        return myprovider.rx.request(API.loadDataDetail())
            .asObservable()
            .retry()
            .observeOn(MainScheduler.instance)
            .filterSuccessfulStatusAndRedirectCodes()
            .catchObjectError()
            .mapObject(ObJectOneClassResult.self)
            .map({ (response) -> ObJectOneClass in
                if ObJectOneClass.data != nil {
                    if let item = response.data {
                        return item
                    }
                    
                    return ObJectOneClass()
                }
                
                return ObJectOneClass()
            })
    } 

func dataTwoObservable() -> Observable<ObJectTwoClass> {
            
            return myprovider.rx.request(API.loadDataProfile())
                .asObservable()
                .retry()
                .observeOn(MainScheduler.instance)
                .filterSuccessfulStatusAndRedirectCodes()
                .catchObjectError()
                .mapObject(ObJectTwoClassResult.self)
                .map({ (response) -> ObJectTwoClass in
                    if ObJectTwoClass.data != nil {
                        if let item = response.data {
                            return item
                        }
                        
                        return ObJectTwoClass()
                    }
                    
                    return ObJectTwoClass()
                })
        } 

然后 我想通过使用 RxSwift 的 combineLastest 来合并结果 但是当我使用 .subscribe 我的响应事件无法传递结果

我的函数调用与此相同:

    func testCombine(completion:@escaping(_ result:Result<(ObJectOneClass,ObJectTwoClass),Error>) -> ()){
        
    

        _ = Observable.combineLatest(dataOneObservable(), dataTwoObservable())
                .asObservable()
                .subscribe({ event in
    //Event<(ObJectOneClass,ObJectTwoClass)>
//case .next((a, b)):
                    switch event{
                        
                    case .next(response):
                        completion(.success(response))
                    case let .error(error):
                        completion(.failure(error as NSError))
                    default:
                        break
                    }
                })
        }

然后

请帮我指导语法补全。

好的,我的主题可以通过declare let in .next

解决问题
func testCombine(completion:@escaping(_ result:Result<(ObJectOneClass,ObJectTwoClass),Error>) -> ()){
        
    

        _ = Observable.combineLatest(dataOneObservable(), dataTwoObservable())
                .asObservable()
                .subscribe({ event in

                    switch event{
                        
                    case .next((let one,let two)):
                        completion(.success((one,two)))
                    case let .error(error):
                        completion(.failure(error as NSError))
                    default:
                        break
                    }
                })
        }

这是一个有趣的解决方案:

func testCombine(completion: @escaping(_ result: Result<(ObJectOneClass, ObJectTwoClass), Error>) -> ()) {
    _ = Observable.combineLatest(dataOneObservable(), dataTwoObservable())
        .map(Result.success)
        .catch(combine(Result.failure, Observable.just))
        .subscribe(onNext: completion)
}

func combine<A, B, C>(_ f: @escaping (A) -> B, _ g: @escaping (B) -> C) -> (A) -> C {
    { g(f([=10=])) }
}

映射到 Result.success,仅捕获 Result.failure 在处理结果类型时非常常见。它是如此常见,以至于您可能想要让一个运算符来捕捉这个概念。

extension Observable {
    func toResult() -> Infallible<Result<Element, Error>> {
        map(Result.success)
            .asInfallible(onErrorRecover: combine(Result.failure, Infallible.just))
    }
}

为了回应您的评论,这里有一个不太高级的版本:

extension Observable {
    func toResult() -> Infallible<Result<Element, Error>> {
        map(Result.success)
            .asInfallible(onErrorRecover: { Infallible.just(Result.failure([=12=])) })
    }
}