URLSession is returning cached byte range responses for full request

If I make a request for a file with the "Range" header specified using iOS's URLSession then it will correctly give me this range. However, if I then request the same file without a "Range" header, I receive the same response as the ranged request. This is the case even if I request a different byte range on the same file.

Here's an example:

let url = "http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/3340/33409.ts"
let session = URLSession.shared
let u = URL(string:url)!
var request = URLRequest(url: u)
request.addValue("bytes=10-19", forHTTPHeaderField: "Range")
session.dataTask(with: request) {(data, response, error) in
    let response = response as! HTTPURLResponse
    print("Ranged with response: \(response.statusCode) \(response.allHeaderFields)")
    var request = URLRequest(url: u)
    request.addValue("bytes=20-49", forHTTPHeaderField: "Range")
    session.dataTask(with: request) {(data, response, error) in
        let response = response as! HTTPURLResponse
        print("Full request with response: \(response.statusCode) \(response.allHeaderFields)")
    }.resume()
}.resume()

I'd like to be able to request a range and then another range later, on the same file. Is there any way to do this without turning off local caching completely?

Replies

This is a known limitation of NSURLCache. I discussed this in this thread on the old DevForums this post. Alas, things have not improved since then.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

>> This is a known limitation of NSURLCache. I discussed this in this thread on the old DevForums. Alas, things have not improved since then.


I have the same issue with Cache and Range header. Could you please update your link " this thread" ? I need confirmation that NSURLCache doesn't work and I need to search workaround for byte seek request.

Could you please update your link … ?

Absolutely. That link was to the old DevForums, which is now shut down. I’ve taken the relevant content, edited it a bit, and put it in a new post.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

But you can use multiple ranges