Swift 5: Return 元组可观察到 RxSwift

Swift 5: Return a tuple in observable with RxSwift

我想return一个元组使用可观察但 我在最后 return 行中收到此错误:“无法将类型为‘(PublishSubject, Bool)’的 return 表达式转换为 return 类型 'Observable<(MessageStatus, Bool)>'” 结构消息状态{ 让消息:字符串 让代码: Int }

class ServiceNetwork: Service {
    
    func getStatus() -> Observable<(MessageStatus, Bool)>{
        
        let query = Bundle.main.query ?? ""
        let responseMessage =  PublishSubject<MessageStatus>()
        let isSuccess = PublishSubject<Bool>()
                
        let body = ["query": "\(query)"]
        
        _ = response(body: body)
            
            .map { try JSONDecoder().decode(Data.self, from: [=12=]) }
        
            .subscribe(onNext: { response in
                guard response.data != nil else {
                    let error = response.errors?.data?.first
                    let failure = MessageStatus(message: error.message , code: error.code)
                    responseMessage.onNext(failure)
                    isSuccess.onNext(false)
                    return
                }
                let sucess = MessageStatus(message: "Success", code: 200)
                responseMessage.onNext(success)
                isSuccess.onNext(true)
                
            }) return (responseMessage, isSuccess) // error here
    }
}

你必须return Observable 流。可以像下面的例子那样做。

class ServiceNetwork: Service {
    
    func getStatus() -> Observable<(MessageStatus, Bool)>{
        Observable.create { subscriber in
            let query = Bundle.main.query ?? ""                    
            let body = ["query": "\(query)"]
        
            let disposable = response(body: body)
            
                .map { try JSONDecoder().decode(Data.self, from: [=10=]) }
        
                .subscribe(onNext: { response in
                    guard response.data != nil else {
                        let error = response.errors?.data?.first
                        let failure = MessageStatus(message: error.message , code: error.code)

                        subscriber.onNext((failure, false))
                        return
                    }
                    let success = MessageStatus(message: "Success", code: 200)
                    subscriber.onNext((success, true))
                
                })
             
            return Disposables.create {
                disposable.dispose()                
            }
        }
    }
}

小建议,想想这个函数之外的错误处理。并且,另外,处理 onError 案例。

只需将 subscribe 换成 map。类似下面的内容:

func getStatus() -> Observable<(MessageStatus, Bool)> {
    let query = Bundle.main.query ?? ""
    let body = ["query": "\(query)"]
    
    return response(body: body)
        .map { try JSONDecoder().decode(Data.self, from: [=10=]) }
        .map { response in
            guard response.data != nil else {
                let error = response.errors?.data?.first
                let failure = MessageStatus(message: error.message , code: error.code)
                return (failure, false)
            }
            let success = MessageStatus(message: "Success", code: 200)
            return (success, true)
        }
}