In my app, there is a background download session. There are a lot of different tasks in this session and I want to be able to resume the tasks if the user quits while it is currently downloading. I don't know what is the best way to achieve this, I'm sure someone more experimented knows how to do this!
I have tried to recreate the download session using the same identifier, but I get this error:
Error Domain=NSCocoaErrorDomain Code=4 "“CFNetworkDownload_T95Ew7.tmp” couldn’t be moved to “medias” because either the former doesn’t exist, or the folder containing the latter doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/[username]/Library/Developer/CoreSimulator/Devices/18FAD568-1671-4212-A2D9-CA6DDA8F9B8F/data/Containers/Data/Application/AA90AA34-3B3F-443D-AB17-6220EBE09C1F/Library/Caches/com.apple.nsurlsessiond/Downloads/com.albinmichel.avantpremiere.AVP/CFNetworkDownload_T95Ew7.tmp, NSUserStringVariant=(
Move
), NSDestinationFilePath=/Users/[username]/Library/Developer/CoreSimulator/Devices/18FAD568-1671-4212-A2D9-CA6DDA8F9B8F/data/Containers/Data/Application/3E9C5227-27FF-4A12-A0F4-CF5BA1402B1A/Documents/medias/38041.jpg, NSFilePath=/Users/[username]/Library/Developer/CoreSimulator/Devices/18FAD568-1671-4212-A2D9-CA6DDA8F9B8F/data/Containers/Data/Application/AA90AA34-3B3F-443D-AB17-6220EBE09C1F/Library/Caches/com.apple.nsurlsessiond/Downloads/com.albinmichel.avantpremiere.AVP/CFNetworkDownload_T95Ew7.tmp, NSUnderlyingError=0x600002ccae20 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
After debugging I have seen that the problem comes from the temp files that are missing. It's weird because it seems to be downloading since it takes some time to process each task.
After some research I have found that when the user kills the app, all download tasks get cancelled, I'm not sure if that is true... If that is, should I have some kind of persistent variable that checks whether the user killed the app while downloading and create the task again when the user launches the app?
Or is it simply a bad practice and a totally normal thing that the downloads gets cancelled and I should wait for the user to click the download button again instead of relaunching it automatically?