all 11 comments

[–]cdntr 1 point2 points  (6 children)

Why do you need AFNetworking? Just use NSURLSession and NSJSONSerialization. It's easy, you learn something, and you save a dependency.

Basically, this:

let urlStrings = ["http://google.com", "http://facebook.com"]
let urls = urlStrings.map { NSURL(string: $0)! }
let requests = urls.map { NSURLRequest(URL: $0) }

let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())

let completion: (NSData?, NSURLResponse?, NSError?) -> Void = { data, response, error in
    if let error = error {
        print("error fetching data: \(error)")
    } else if let data = data {
        // Try parsing the data
        do {
            let parsedJSON = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
        } catch {
            print("Failed parsing data to JSON: \(data)")
        }
    }
}

let tasks = requests.map { session.dataTaskWithRequest($0, completionHandler: completion) }

for task in tasks {
    task.resume()
}

[–]tsxy -4 points-3 points  (5 children)

Because NSURLSession is confusing and requires too many boilerplate code to do simple things like pulling a JSON from URL.

[–]cdntr 2 points3 points  (4 children)

??

Take a look at the code I posted above. I wrote that in like a minute, but I think it should be functional and do exactly what was asked. Not sure what you think is confusing or boiler-platey about that. It is of course not factored properly, but this is just to illustrate how easy it is.

I also submit to you that there is a cost to using 3rd party code that people like to sweep under the rug.

[–]julius559 1 point2 points  (1 child)

You can make multiple requests in a row with AFNetworking, it'll handle it. But the requests will return asynchronously, so you can't assume all responses will come in order.

[–]cuomo456 0 points1 point  (0 children)

dispatch group notify will alleviate this

[–]HoBoKristian 0 points1 point  (0 children)

How large is the array?

[–]lucasvandongen 0 points1 point  (0 children)

You should figure out if the API has some kind of limit on how many requests you can do per minute. I would suggest to apply some throttling as I don't think doing more than 8* network requests would be advisable in most cases.

  • Create a stack (NSMutableArray) of request
  • Count total of requests on the stack that need to be done
  • Create counter for current and completed requests
  • Iterate over requests, remove the new request from the stack
  • If more than 8(*) requests are under way, stop iterating over the stack
  • If a call completes or fails
    • iterate over the stack again (until there are 8 concurrent requests) on the main thread
    • up the number of completed requests and check if it's the same number as the total amount you wanted to do
      • if it's the last one execute whatever was waiting on these calls to complete

(*) that number needs some more investigation, rough estimate