使用RxSwift执行操作的顺序

时间:2015-09-23 17:58:55

标签: swift reactive-programming rx-swift

我正在使用RxSwift来完成我的代码。对于我目前的项目,我想将RxSwift的原则应用于来自LayerKit的一堆完成块:

layerClient.connectWithCompletion { (success, error) -> () in
  if (!success) {
     // Error     
  } else {
    layerClient.requestAuthenticationNonceWithCompletion { (nonce, error) -> () in
      // Even more blocks
    }
  }
}

我正在考虑这样的事情:

// In extension
public func rx_connect() -> Observable<Bool> {
    return create { observer in

        self.connectWithCompletion { (success, error) -> ()in
            if (success) {
                observer.on(.Next(success))
                observer.on(.Completed)
            } else {
                observer.on(.Error(error))
            }
        }
        return NopDisposable.instance
    }
} 

public func rx_requestAuthenticationNonce() -> Observable<String> {
    // Same for annother method
}

// In AppDelegate
self.layerClient.rx_connect()
 .then() // requestAuthenticationNonceWithCompletion and use the nonce for next action
 .then()
 .subscribeNext(…
 .onError(… // To catch all errors

RxSwift没有then()方法。有没有其他方法可以做这个链接的东西,或者我在如何使用ReactiveX一般认为错误?

2 个答案:

答案 0 :(得分:6)

是的,RxSwift没有then运算符,因为名称风险很大。

then可以是Reactive Extensions世界中的很多内容map a flatMap甚至是switchLatest,具体取决于具体情况。

在这种情况下,我建议使用flatMap,因为将使用map返回一个Observable of Observables,在大多数情况下难以组合和管理。所以我会这样做:

self.layerClient.rx_connect()
 .flatMap(){ _ in
   return rx_requestAuthenticationNonce()
             .catchError(displayError)
 }
 .subscribeNext(…
 .onError(… // To catch all errors

在这种情况下,我会使用flatMap而不是map因为我能够在rx_requestAuthenticationNonce()失败的情况下捕获错误而不会终止序列。

使用catchError运算符时要小心,此运算符将在恢复后终止序列,之后将忽略任何额外事件。

答案 1 :(得分:2)

这是我项目中的授权部分。只是链接请求的示例。它将链式请求的结果转换为bool答案。它使用'flatMap',因为旧请求的结果用于构建新请求。否则,您可以使用'concat'而不是'flatMap'。

       let client = RxSimpleLazyHttpClient.sharedInstance
       let uuid = UIDevice().identifierForVendor!.UUIDString

       let helloheaders = ["X-User-Identifier": "id:" + uuid]
       let helloRequest: Observable<StringDictionary> = client.makeRequest(verb: .GET, url: NSURL(string: self.API_HOST + self.API_HELLO)!, parameters: [:], headers: helloheaders)
            .map { result in
                if (self.enableDebugOutput) {
                    self.debug(result)
                }

                let json = JSON(data: result.2!)
                let userKey = json["user_key"].string
                let userSecretHash = json["user_secret_hash"].string

                return ["user_key": userKey ?? "", "user_secret_hash": userSecretHash ?? ""]
            }

        let jointAuthRequest: (StringDictionary -> Observable<StringDictionary>) = { [unowned self] stringDictionary in
            Logger.D(stringDictionary.debugDescription)

            let userKey = stringDictionary["user_key"]
            let headers = ["X-User": userKey ?? ""]
            return client.makeRequest(verb: .GET, url: NSURL(string: self.API_HOST + self.API_AUTH)!, parameters: [:], headers: headers)
                .map { result in

                    if (self.enableDebugOutput) {
                        self.debug(result)
                    }

                    let json = JSON(data: result.2!)
                    let nonce = json["nonce"].string
                    var result = [String: String]()
                    result["nonce"] = nonce

                    return result.merge(stringDictionary)
                }
        }

        let jointAuthNonceRequest: (StringDictionary -> Observable<StringDictionary>) = { [unowned self] stringDictionary in
            Logger.D(stringDictionary.debugDescription)

            let nonce = stringDictionary["nonce"] ?? ""
            let userSecretHash = stringDictionary["user_secret_hash"] ?? ""
            let headers = ["X-Pass": (userSecretHash + nonce).sha1().webSafeBase64()]

            return client.makeRequest(verb: .GET, url: NSURL(string: self.API_HOST + self.API_AUTH + "/" + nonce)!, parameters: [:], headers: headers)
                .map { result in

                    if (self.enableDebugOutput) {
                        self.debug(result)
                    }

                    let json = JSON(data: result.2!)
                    let sessionKey = json["session_key"].string
                    let sessionSecret = json["session_secret"].string
                    var result = [String: String]()
                    result["session_key"] = sessionKey
                    result["session_secret"] = sessionSecret
                    return result.merge(stringDictionary)
                }
        }

        let jointResult: (StringDictionary -> Observable<Bool>) = { result in
            let userKey = result["user_key"]
            let userSecretHash = result["user_secret_hash"]
            let sessionKey = result["session_key"]
            let sessionSecret = result["session_secret"]

            if userKey == nil || userSecretHash == nil || sessionKey == nil || sessionSecret == nil {
                Logger.D("Auth Fail")
                return just(false)
            }

            /* You can store session key here */

            return just(true)

        }

        return helloRequest
            .shareReplay(1)
            .observeOn(RxScheduler.sharedInstance.mainScheduler)
            .flatMap(jointAuthRequest)
            .flatMap(jointAuthNonceRequest)
            .flatMap(jointResult)

这是'makeRequest'方法。

public func makeRequest(verb verb: Alamofire.Method, url: NSURL, parameters: [String : String]?, headers: [String : String]?) -> Observable<RxRequestResult> {
    return create { observer in
        Alamofire.request(verb, url, parameters: nil, encoding: ParameterEncoding.URL, headers: headers)
            .response { request, response, data, error in
                observer.onNext((request, response, data, error))
            }
        return AnonymousDisposable {
            // when disposed
        }
    }
}