-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
There's a gotcha in this code 😨 #1
Comments
Thanks for the report. However, I cannot reproduce the problem. Did you run the project from this repo or did you make yours following the article? The problem you have might be caused by the thread where the continuation runs. In my test, the That means that, at that point, the If you created your own project, you might have forgotten to mark the The solution is making sure that
|
Hey @matteom, sorry for the huge delay to your reply. Just snowed under 👪. Thanks for your suggestions. From memory I experienced it in my own code, and then went to try the sample code for the blog post. This is a bit outlandish but here's a video of the few steps I just took now 🎥 Video: http://172.104.253.215/CleanShot-2023-06-02-at-13.01.10-converted.mp4 Basically:
one wrinkle is that the first download didn't log an error, but subsequent downloads all logged the following:
|
If I make two changes to add print statements: func saveFile(for episode: Episode, at url: URL) {
print("Exists in `ViewModel`?: ", FileManager.default.fileExists(atPath: url.path())) and func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Exists in `Download`? : ", FileManager.default.fileExists(atPath: location.path())) I (almost) always get:
thought occasionally both have been |
I'm facing the same problem, and the "main thread solutions" not work either. And then I tried to solve it by a tricky way. |
Really loved the blog post, however there's actually a gotcha in this approach using continuations.
Unfortunately the
location
is "use it or lose it" .. before the function exits scope. Otherwise the file at this temporary location no longer exists.docs
So if we modify
ViewModel.saveFile(for episode: Episode, at url: URL)
to actually catch the error:Then the error is revealed:
Basically the above function calls
continuation?.yield(.success(url: location))
and then returns, before the ViewModel has a chance to actually handle the next event (i.e. before it has a chance to move the file out of the temporary location). So the file is gone.Quite heartbreaking because this was otherwise a very nice approach. 💔
Not sure if there's a way to tweak it so that the continuation is handled synchronously, I'm not that advanced with async/await yet.
The text was updated successfully, but these errors were encountered: