-
Notifications
You must be signed in to change notification settings - Fork 4
Description
Really loved the blog post, however there's actually a gotcha in this approach using continuations.
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
Unfortunately the location
is "use it or lose it" .. before the function exits scope. Otherwise the file at this temporary location no longer exists.
So if we modify ViewModel.saveFile(for episode: Episode, at url: URL)
to actually catch the error:
Then the error is revealed:
error: Error Domain=NSCocoaErrorDomain Code=4 "“CFNetworkDownload_45xjff.tmp” couldn’t be moved to “1386867488” because either the former doesn’t exist, or the folder containing the latter doesn’t exist." UserInfo=...., NSUnderlyingError=0x600001f0a550 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
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.