Can progress reporting be added to this style of https transfer?

import Foundation

func fetchData() async throws -> Data {
let url = URL(string: "https://gist.github.com/khaykov/a6105154becce4c0530da38e723c2330/raw/41ab415ac41c93a198f7da5b47d604956157c5c3/gistfile1.txt")!

var request = URLRequest(url: url)

let (data, _) = try await URLSession.shared.data(for: request)
return data
}

@main
struct Main {
static func main() async throws {
do {
let foo = try await fetchData()
let bar = String(data: foo, encoding: .utf8)
dump(bar)
} catch {
print("** \(error.localizedDescription)")
}
}
}
import Foundation

func fetchData() async throws -> Data {
let url = URL(string: "https://gist.github.com/khaykov/a6105154becce4c0530da38e723c2330/raw/41ab415ac41c93a198f7da5b47d604956157c5c3/gistfile1.txt")!

var request = URLRequest(url: url)

let (data, _) = try await URLSession.shared.data(for: request)
return data
}

@main
struct Main {
static func main() async throws {
do {
let foo = try await fetchData()
let bar = String(data: foo, encoding: .utf8)
dump(bar)
} catch {
print("** \(error.localizedDescription)")
}
}
}
I've asked several AI chatbots and none of their attempts have worked. I'm guessing I should be using the old dataTask ways to do this and that trying to do it with async/await isn't really implemented/supported yet. But even though I'm an old programmer I'm very new to the Apple ecosystem.
2 Replies
hippietrail
hippietrail13mo ago
the code I started with was using a semaphore and I either assumed or read that was the old way and these days I should use async/await. Other examples I can find are for GUI apps (I'm doing commandline); or they used RunLoop, which I also assumed would be outdated I'm assuming i'm going to need a delegate. but so for i've only used those in gui apps and i haven't found an example yet how to make/use one for my case the answer is yes instead of
let (data, _) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
use
let (asyncBytes, response) = try await URLSession.shared.bytes(from: url)
let (asyncBytes, response) = try await URLSession.shared.bytes(from: url)
then you can read the bytes byte-by-byte or line-by-line or probably other ways
for try await byte in bytes {
for try await byte in bytes {
or
for try await lines in bytes.lines {
for try await lines in bytes.lines {
then you can report progress either for each one, or keep track of how many calls/bytes/lines/etc or time elapsed to decide how often report progress
hippietrail
hippietrail13mo ago
but it seems to actually be less effort to use the old continuation method and both ways are equally fast. here's the great answer I got on Stack Overflow https://stackoverflow.com/a/77072098/527702
Stack Overflow
Modern way for macOS commandline code to fetch data in Swift, with ...
Old programmer but new to Apple ecosystem. I want to use the most modern APIs and Swift techniques to fetch some data over https and get progress reports. Most tutorials and examples I can find use
Want results from more Discord servers?
Add your server