链接多个 Alamofire 请求

编程入门 行业动态 更新时间:2024-10-25 18:32:53
本文介绍了链接多个 Alamofire 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一种可以链接多个 HTTP 请求的良好模式.我想使用 Swift,最好是 Alamofire.

I'm looking for a good pattern with which I can chain multiple HTTP requests. I want to use Swift, and preferrably Alamofire.

比如说,我想做以下事情:

Say, for example, I want to do the following:

  • 发出 PUT 请求
  • 发出 GET 请求
  • 用数据重新加载表格
  • promises 的概念似乎很适合这种情况.如果我可以做这样的事情,PromiseKit 可能是一个不错的选择:

    It seems that the concept of promises may be a good fit for this. PromiseKit could be a good option if I could do something like this:

    NSURLConnection.promise( Alamofire.request( Router.Put(url: "httbin/put") ) ).then { (request, response, data, error) in Alamofire.request( Router.Get(url: "httbin/get") ) }.then { (request, response, data, error) in // Process data }.then { () -> () in // Reload table }

    但那是不可能的,或者至少我不知道.

    but that's not possible or at least I'm not aware of it.

    如何在不嵌套多个方法的情况下实现此功能?

    How can I achieve this functionality without nesting multiple methods?

    我是 iOS 新手,所以也许我缺少一些更基本的东西.我在Android等其他框架中所做的是在后台进程中执行这些操作并使请求同步.但是Alamofire 本质上是异步的,所以这种模式不是一种选择.

    I'm new to iOS so maybe there's something more fundamental that I'm missing. What I've done in other frameworks such as Android is to perform these operations in a background process and make the requests synchronous. But Alamofire is inherently asynchronous, so that pattern is not an option.

    推荐答案

    在 Promise 中包装其他异步内容的工作方式如下:

    Wrapping other asynchronous stuff in promises works like this:

    func myThingy() -> Promise<AnyObject> { return Promise{ fulfill, reject in Alamofire.request(.GET, "httpbin/get", parameters: ["foo": "bar"]).response { (_, _, data, error) in if error == nil { fulfill(data) } else { reject(error) } } } }

    现在,使用:github/PromiseKit/Alamofire-

    更多推荐

    链接多个 Alamofire 请求

    本文发布于:2023-11-26 03:28:39,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1632452.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:多个   链接   Alamofire

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!