RxMoya失败,错误域= NSURLErrorDomain代码= -999“已取消”

时间:2019-07-10 15:08:04

标签: ios swift moya

所以我有一个基类,我的服务类可以从该基类继承来执行请求。下面是一个代码段。

import Foundation
import Moya
import RxSwift

class BaseServiceRespository<Target: TargetType> {

    private let disposeBag = DisposeBag()
    private var provider: MoyaProvider<Target>!

    init(_  provider: MoyaProvider<Target>!) {
        self.provider = provider
    }

    func execute<T: Codable>(target: Target,
                             mapTo type: T.Type,
                             mapping: CodableMappingStrategy?) -> Observable<ServiceRespositoryResult<T>>  {

        return Observable.create { observer in

            self.provider
                .rx
                .request(target)
                .baseObservable()
                .filterSuccessfulStatusCodes()
                .subscribe(onNext: { response in

                    guard let mapping = mapping else {
                        observer.onNext(.success(ResponseObject(response: response, data: try? response.map(T.self))))
                        return
                    }

                    guard let decodedObj = try? response.map(T.self, atKeyPath: mapping.keypath, using: mapping.decoder, failsOnEmptyData: mapping.failsOnEmpty) else {
                        observer.onNext(.success(ResponseObject(response: response, data: nil)))
                        return
                    }

                    observer.onNext(.success(ResponseObject(response: response, data: decodedObj)))

                }, onError: { error in
                    observer.onNext(.failure(error))
                }).disposed(by: self.disposeBag)

            return Disposables.create()
        }
    }

    func executeArr<T: Codable>(target: Target,
                             mapTo type: T.Type,
                             mapping: CodableMappingStrategy?) -> Observable<ServiceRespositoryResultArray<T>>  {

        return Observable.create { observer in

            self.provider
                .rx
                .request(target)
                .baseObservable()
                .filterSuccessfulStatusCodes()
                .subscribe(onNext: { response in

                    guard let mapping = mapping else {
                        observer.onNext(.success(ResponseObject(response: response, data: try? response.map(Array<T>.self))))
                        return
                    }

                    guard let decodedObj = try? response.map(Array<T>.self, atKeyPath: mapping.keypath, using: mapping.decoder, failsOnEmptyData: mapping.failsOnEmpty) else {
                        observer.onNext(.success(ResponseObject(response: response, data: nil)))
                        return
                    }

                    observer.onNext(.success(ResponseObject(response: response, data: decodedObj)))

                }, onError: { error in
                    observer.onNext(.failure(error))
                }).disposed(by: self.disposeBag)

            return Disposables.create()
        }
    }
}

因此,如果我想将此类用于我的服务,我所要做的就是像这样在我的服务类中将其子类化,然后通过rx中的observable订阅实际的请求。像下面这样。

import Foundation
import RxSwift

class LoginService: BaseServiceRespository<AccountEndPoint> {

    private let disposeBag = DisposeBag()

    func login(_ credentials: LoginCredentials) {

       self.execute(target: .login(config: networkConfig, credentials: credentials), mapTo: String.self, mapping: CodableMappingBuilder().add(failOnEmpty: false).build())
        .subscribe(onNext: { res in

            switch res {

            case .success(let response):
                print(response.data)
            case .failure(let error):
                print(error)
            }

        }).disposed(by: disposeBag)
    }
}

因此,为了测试它是否有效,我只是想在viewDidLoad中执行此代码

LoginService.init(ServiceProviderBuilder<AccountEndPoint>().debug(verbose: true, cURL: true).build())
                        .login(LoginCredentials(emailAddress: "xxxxx", password: "xxxxx"))

我遇到的问题是,moya似乎开始了该请求,但随后取消了该请求,我不太确定为什么。我似乎从控制台获得的所有内容如下(我只显示允许的控制台信息。)。但是我绝对不会取消该请求,这让我有点困惑。

1> load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=xxxxx, NSErrorFailingURLKey=xxxxx, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <91E9466B-7410-4E01-8FF4-CC314FC81BC5>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <91E9466B-7410-4E01-8FF4-CC314FC81BC5>.<1>, NSLocalizedDescription=cancelled} [-999]

0 个答案:

没有答案