|
Hello everyone, my app downloads a couple of files from a server, using a This function is called after another My code (Swift 5, Xcode 14.2):
There are two things that I'm experiencing problems with:
I thought that this is exactly what a |
|
In URLSession.dataTask, everything between the first brace and its matching closing brace is a "completion handler". Contrary to your print statement, it does not run when you start the task. It runs after the session completes its operation and returns either the data or an error. If this is a GUI app, you do not want a loop waiting for data. Instead, after task.resume() initiates the http session, fileDownload() should immediately return to the code that called it. However, instead of learning to use "completion handlers", a more modern approach would be for you to learn Swift Concurrency, more commonly referred to as async/await. Both Paul Hudson and Donny Wals have excellent eBooks on Swift Concurrency. |
|
Yes, I know that the task is started with Also yes, this is a GUI app but I display a progress popup while downloading, which includes the name of the file that's currently being downloaded. The app can't be used without that data, so people have to wait anyway, I just don't want to block the main thread, which might be what happens with I know about async/wait, the problem is that it can only be used with I've played around with semaphores a bit after writing the post and I think I got it (iirc) but people advice against using those, so I'm back to square one. Have you got a suggestion how I can do the downloads, as mentioned above, with the |
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.