RxSwift Procedure

I use RxSwift only for my code. For my current project, I would like to apply RxSwift principles to a mess of completion blocks from LayerKit:

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

I am thinking of something like this:

// 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 has no method then(). Is there any other way to make this chain or am I not mistaken in how to use ReactiveX in general?

+4
source share
2 answers

To the right, RxSwift does not have an operator thenbecause this name is very risky.

thenthere can be many things in the world of Reactive Extensions a mapa flatMapor even switchLatest, depending on the context.

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, , .

+6

. . 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
        }
    }
}
+2

Source: https://habr.com/ru/post/1608715/


All Articles