# URLSession download of a large file took ages

**URL:** <https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061>\
**Category:** Using Swift\
**Tags:** urlsession, macos\
**Created:** [April 3, 2024, 4:05pm UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061 "2024-04-03T16:05:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![KrisSimon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krissimon/32/33284_2.png) [@KrisSimon](https://forums.swift.org/u/KrisSimon)\
**Post date:** [April 3, 2024, 4:05pm UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/1 "2024-04-03T16:05:43Z")

</div>

Hi,

can someone tell why downloading the same file in Safari is fast (8sec), but from within my script it takes ages (40min)?

```swift
    private func download() async -> Data? {
        do {
            let configuration = URLSessionConfiguration.default
            configuration.timeoutIntervalForRequest = .infinity
            configuration.requestCachePolicy = .returnCacheDataElseLoad
            
            let session = URLSession(configuration: configuration)
            
             let (asyncBytes, urlResponse) = try await session.bytes(from: resourcesURL)
             let length = (urlResponse.expectedContentLength)
             let gb_total = Double(length) / 1024 / 1024 / 1024
            
             var data = Data()
             data.reserveCapacity(Int(length))
            
            for try await byte in asyncBytes {
                 data.append(byte)
                 
                 let progress = Double(data.count) / Double(length)
                 
                 let gb = Double(data.count) / 1024 / 1024 / 1024
                 let formatted_gb_total = String(format: "%.3f", gb_total)
                 let formatted_gg_loaded = String(format: "%.3f", gb)
                 
                 
                 taskLabel.stringValue = "Downloading (\(formatted_gg_loaded) of \(formatted_gb_total) GB)\(getDot(for: data.count))"
                 progressBar.doubleValue = progress
             }
             return data
         } catch {
             return nil
         }
    }

```

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 3, 2024, 4:23pm UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/2 "2024-04-03T16:23:10Z")

</div>

- How big is the file in question?
- I'd first check that `expectedContentLength` returns a sensible result.
- Then I'd try to comment out label/progress bar updating to see how that affects the timing.
- Then check the timing with formatting removed as well.
- Then with `data.append` commented out.
- Then checking the timing if you switch from "bytes" to "data" (to grab the whole data at once).
- Then switching from "async" versions of those to a callback.

I guess one of the steps above will reveal the culprit and you'd know where to go from there.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [April 3, 2024, 5:16pm UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/3 "2024-04-03T17:16:40Z")

</div>

You’re streaming byte by byte. Unless the optimizer likes you this can very slow. Better to use `URLSession`’s actual download method.

---

<div class="post-metadata">

**Author:** ![KrisSimon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krissimon/32/33284_2.png) [@KrisSimon](https://forums.swift.org/u/KrisSimon)\
**Post date:** [April 3, 2024, 6:20pm UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/4 "2024-04-03T18:20:11Z")

</div>

yap, i undeerstand and see it. Thanks a lot. Download-task and delegates are even more the code that i like. ;)

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 3, 2024, 6:41pm UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/5 "2024-04-03T18:41:46Z")

</div>

> [@Jon\_Shier](#):
>
> You’re streaming byte by byte. Unless the optimizer likes you this can very slow.

Yes... but 300x slowdown?! If I read a file byte by byte with `fread` it could be, say, 3x time slower overall than reading reading it with `fread` by 100K blocks, 300x would be quite unexpected; I expect the same with URLSession.

> [@Jon\_Shier](#):
>
> Better to use `URLSession`’s actual download method.

That's a very good advice. Note that your app doesn't even have to be running for the download to happen – once the download is finished your app will be relaunched if needed.

---

<div class="post-metadata">

**Author:** ![KrisSimon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krissimon/32/33284_2.png) [@KrisSimon](https://forums.swift.org/u/KrisSimon)\
**Post date:** [April 3, 2024, 6:56pm UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/6 "2024-04-03T18:56:22Z")

</div>

Ok, thanks for your help.  
I have to admit, that I cant get URLSession working with delegates correctly. The Task did not start. So I switch over to what I know: `AsyncHTTPClient` works as it should.

Hm, so URLSession did not work correctly on Linux, and on MacOS it's hard to get it working correctly. 🤔 (at least for me). So I think I stick to AsyncHTTPClient, is there is any downside?

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 3, 2024, 7:27pm UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/7 "2024-04-03T19:27:35Z")

</div>

You don't need the "delegate" version specifically... I suggested to try with a callback version to see if there's a significant speed difference.

The typical gotcha is forgetting calling `resume()`:

```swift
URLSession.shared.dataTask(with: request) { data, response, error in
    // data or error here
}.resume() // !

```

similar for `downloadTask`, you are just getting the temporary file URL in the callback instead of data.

The thing I was talking above was a combination of using `downloadTask` and "background" session:

```swift
URLSession(configuration: .background(withIdentifier: "myBackgroundSession"))

```

I can't comment on AsyncHTTPClient or using URLSession on linux as I never tried those.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 3, 2024, 10:52pm UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/8 "2024-04-03T22:52:21Z")

</div>

I assume you need the file at the end of the process, not necessarily Data in memory? If so use downloadTask, this will save you of extra work of writing the accumulated data to a file. Here's downloadTask example with progress tracking:

```swift
class Test: NSObject, URLSessionDownloadDelegate {
    func download(_ url: URL) {
        let session = URLSession(configuration: .default, delegate: self, delegateQueue: .main)
        session.downloadTask(with: URLRequest(url: url)).resume()
    }
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        print("finihsed \(location)")
    }
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        print("\(totalBytesWritten/1024/1024)MB of \(totalBytesExpectedToWrite/1024/1024)MB")
    }
}

```

Note that for some URL resources `totalBytesExpectedToWrite` (taken from response's expectedContentLength would be -1, be ready to handle that case correctly (e.g. with an indefinite progress).

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 4, 2024, 3:08am UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/9 "2024-04-04T03:08:22Z")

</div>

> [@tera](#):
>
> but 300x slowdown?!

Never assume! [This test](https://forums.swift.org/t/what-is-the-best-way-to-work-with-the-file-system/71020/18) shows 7600x slowdown in one of the cases!

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [April 4, 2024, 3:17am UTC](https://forums.swift.org/t/urlsession-download-of-a-large-file-took-ages/71061/10 "2024-04-04T03:17:52Z")

</div>

> [@tera](#):
>
> Yes... but 300x slowdown?! If I read a file byte by byte with `fread` it could be, say, 3x time slower overall than reading reading it with `fread` by 100K blocks, 300x would be quite unexpected; I expect the same with URLSession.

It's allocating three Strings for every byte in the download, I'm surprised it's _just_ 300x slower
