多个Alamofire请求

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

我正在构建一个将列出所有用户instagram关注者的应用。 Instagram API仅以JSON数组的形式返回大约50个关注者,并返回到下50个关注者的链接,依此类推,直到发送所有关注者为止。这种方法称为分页。

I'm building an app that will list all of a user instagram followers. Instagram API returns only around 50 followers as JSON array and a link to the next 50 and so on till all the followers are sent. This method is called pagination.

我要执行的操作是以下

What I'm trying to do is the following

  • 用主API URl
  • 将所有已发送的数据附加到JSON数组
  • 重新发送请求,但将URL设置为分页URL(将列出接下来的50个关注者)
  • 将新发送的关注者追加到JSON数组
  • 继续这样做,直到接下来的50个网址为空(没有更多关注者)
  • Start a request with the a main API URl
  • append all the sent data to a JSON array
  • Re-Send a request but with URL set as the pagination URL( that will list the next 50 followers)
  • append the new sent followers to the JSON array
  • Keep on doing so until the url for the next 50 is empty ( no more followers)


    我正在使用Swift 1.2,Xcode 6.4,Alamofire最新版本,SwiftyJSON最新版本

    I'm using Swift 1.2 , Xcode 6.4, Alamofire lastest release, SwiftyJSON latest Release

    我使用的代码

    Here is the code i used

    do { Alamofire.request(.GET, dataURL).responseJSON { (request, response, json, error) in print("Request Started") if json == nil || error != nil { let alertView = UIAlertController(title: "Error", message: "No Data found. Check your internet connection", preferredStyle: .Alert) alertView.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil)) self.presentViewController(alertView, animated: true, completion: nil) return }else{ var jsonObject = JSON(json!) var dataOfNextURl = jsonObject["pagination"]["next_url"].string! print(" Data for next url :\(dataOfNextURl)") dataURL = dataOfNextURl } } }while(dataURL != "")

    在控制台中,我得到无数的 Request Started打印(无限循环,没有启动请求并等待响应),但是我没有得到打印结果(下一个网址的数据:(dataOfNextURl))已执行。我猜想Alamofire旨在在不同的线程上工作以降低复杂性,但如果我单独发出一个请求,而没有 while 循环,它会按预期工作(等待响应之前 那么我该怎么做呢?让alamofire在循环之前等待每个响应谢谢

    In the console I'm getting endless "Request Started " printed ( infinite loop, not initiating the request and waiting for the response), but I'm not getting an of the print(" Data for next url :(dataOfNextURl)") executed. I'm guessing that Alamofire is designed to work on a different thread for reducing complexity but if i issue a request alone without the while loop it works as supposed( waits for response before executing later code. So how can I do this? make alamofire wait for every response before iterating the loop. Thanks

    推荐答案

    NSUserDefaults为此具有 registerDefaults:方法。请使用它(在 application:didFinishLaunchingWithOptions中: )。

    NSUserDefaults has a registerDefaults: method for this purpose. Use it (in application:didFinishLaunchingWithOptions:).

  • 更多推荐

    多个Alamofire请求

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

    发布评论

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

    >www.elefans.com

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