2

It is unclear as to whether Alamofire supports chunked data for large or progressive data sets. This is a much needed feature for my application, otherwise I may have to look into alternative methods.

On the Alamofire Github page, it states Progress Closure & NSProgress but I'm not sure what that entails.

And per the description on Wikipedia on Chunked data transfer.

Senders can begin transmitting dynamically-generated content before knowing the total size of that content.


For clarity's sake, let me explain why I need this.

Basically I have a very large JSON file that is partially cached. The full JSON file is composed up of smaller JSON objects. I am using iojs / nodejs to send the chunked data via res.write() with Express which knows not to send the Content-Length header AND send it as chunked data. I have verified this works via html/js.


Let me know if you would like for me to provide the code to demonstrate this!

Levi Roberts
  • 1,277
  • 3
  • 22
  • 44

3 Answers3

7

The correct way to handle chunked response in Alamofire is using stream:

    let req = Alamofire.request("http://localhost:8080")

    req.stream { (data) in
       print(String(data: data, encoding: String.Encoding.utf8) ?? "No data")
    }
MatterGoal
  • 16,038
  • 19
  • 109
  • 186
1

Alamofire definitely supports Transfer-Encoding: chunked data due to the fact that it is already supported by NSURLSession. Here is a quick example of downloading a chunked image from httpwatch.com.

let request = Alamofire.request(.GET, "http://www.httpwatch.com/httpgallery/chunked/chunkedimage.aspx")
request.progress { bytesReceived, totalBytesReceived, totalBytesExpected in
    println("\(bytesReceived) - \(totalBytesReceived) - \(totalBytesExpected)")
}
request.response { request, response, _, error in
    println(request)
    println(response)
    println(error)
}

debugPrintln(request)

Since the content length of the image is not available, the totalBytesExpected will always be reported as -1 since it is unknown. The bytesReceived and totalBytesReceived are reported properly though. It seems to me that chunked downloads are probably not the best candidate for trying to present download progress to the user since it is of an undefined length.

Another possible feature that could be of use is the new stream functionality on a request. It allows you to store each data chunk as it is downloaded.

If these options don't suit all your needs, please file an issue on our Github project with the issues you are running into so we can investigate further.

cnoon
  • 16,575
  • 7
  • 58
  • 66
  • 2
    This led me to the answer I was looking for, although not an answer I liked. It looks as if the data isn't available until after a success closure in any of the cases. I was hoping to process JSON data as it was being streamed in chunks. This doesn't look possible right now. Looks like I may have to implement another solution (likely Socket.io) – Levi Roberts Jun 22 '15 at 20:08
  • You can already do this in Alamofire. As I mentioned in my answer, you should look into the `stream` [functionality](https://github.com/Alamofire/Alamofire/blob/master/Source/Request.swift#L120-L135). – cnoon Jun 25 '15 at 01:20
  • 1
    I did look into that however it was unsuitable for my requirement. The objects were occasionally malformed even though the server sent full objects as chunked data. Other times, the event never fired. – Levi Roberts Jun 25 '15 at 02:27
  • @LeviRoberts did you solve your problem? how? I have exactly the same problem as what you described in your comment. – tara tandel Mar 10 '18 at 10:29
-1

Following might help -

https://github.com/Alamofire/Alamofire#downloading-a-file-wprogress

Downloading a File w/Progress

Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
         .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
             println(totalBytesRead)
         }
         .response { (request, response, _, error) in
             println(response)
         }
Tushar
  • 3,022
  • 2
  • 26
  • 26
  • This doesn't really answer my question. Primarily, this is for downloading files - hence the reason the progress closure only gives you an indicator of data size to data downloaded. This isn't a closure that works the same way a chunked response would work. – Levi Roberts Jun 12 '15 at 13:19
  • take a look at this also: http://stackoverflow.com/questions/9296221/what-are-alternatives-to-nsurlconnection-for-chunked-transfer-encoding – Tushar Jun 12 '15 at 13:35
  • Yeah. I saw that before I posted my question. Ideally, I want chunked data in Alamofire so that I won't have to code up my own library for it. Although, I'm beginning to think I may have to. Alternatively, I could send this as a feature request to the author. – Levi Roberts Jun 12 '15 at 13:44