I’m trying to use Supabase on my iOS app.
I have a download button that downloads a PDF file from Supabase server.
I want my button to gradually fill in color(left to right) as download progress.
I’ve been trying to find how to achieve it for several hours, but couldn’t solve it..
I managed to write some codes, but then the downloadProgress was something like 0.0014444 and infinitely downloads..
I’ve looked up Supabase github, but they talked about upload progress, but didn’t mention about download progress. I guess it’s something basic that doesn’t need their support….
func downloadFileWithProgress(bucketId: String, path: String, downloadPath: URL) async {
do {
await MainActor.run { self.downloading = true }
let signedURL = try await client.storage.from(bucketId).createSignedURL(path: path, expiresIn: 60)
let (asyncBytes, response) = try await URLSession.shared.bytes(from: signedURL)
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode),
let contentLength = httpResponse.value(forHTTPHeaderField: "Content-Length"),
let expectedContentLength = Int(contentLength) else {
throw URLError(.badServerResponse)
}
var receivedData = Data()
var receivedBytesCount: Int64 = 0
for try await byte in asyncBytes {
receivedData.append(byte)
receivedBytesCount += 1
let progress = Double(receivedBytesCount) / Double(expectedContentLength)
await MainActor.run {
self.downloadProgress = progress
print("Download percentage: (progress * 100)")
}
}
if receivedBytesCount != expectedContentLength {
throw URLError(.cannotLoadFromNetwork)
}
try receivedData.write(to: downloadPath)
await MainActor.run { self.downloading = false }
} catch {
print("Download error: (error)")
await MainActor.run { self.downloading = false }
}
}
Jae Ho Yoon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.