SKDownload problem after app goes to background

96 Views Asked by At

In my app, the download of content hosted with Apple goes well if the app is active. However, if it goes to background and sufficient time passes, when it becomes active again, SKDownload still has an active state, but no progress is being reported. I have verified that the transaction is not finished yet.

Does anyone know why this occurs? My device is on Wi-Fi and I could verify download's state because I saved SKDownload to an instance variable. So, if it was merely an issue with not receiving download progress in the background, I would still be able to read the current value from myDownload.progress. However, the progress is exactly as it was and it is not being updated.

It is a big problem for me because as mentioned above I do store SKDownload to a variable. I am doing so in order to prevent a duplicate download from starting. My assets are greater than 150MB, so it may well be the case that users do not appreciate how long it may take to download the assets and will attempt to initiate a second download by restoring/"purchasing" item again. So, I store the running download to a variable and when a new transaction completes I check whether the download is already running. It is a great design as it ensures that the download completes in the shortest period of time. The only problem occurs if the app was in background for a long time and then I get this state:

  1. Download reports being active
  2. The corresponding transaction is not yet finished
  3. For some reason, download does not progress further

Given the safeguard described above, the problem is obvious as the download is not running and a new one will not start because the app sees a duplicate active download.

I tried resuming the download, but it does nothing (I guess because the state is not paused).

Update 1

I found a way to solve this problem, but it looks like a hack to me:

  1. Pause the download when the app goes to background
  2. Add a store transaction observer again (!!!) in applicationWillEnterForeground.
  3. Resume paused download

2 is the most interesting part. I have a singleton which is responsible for handling all store-related notifications. I add it as a transaction observer in didFinishLaunchingWithOptions and I remove it only in willTerminate. I verified that the singleton is the same after the app goes to foreground state. Yet, unless I add it as a store observer again, I do not see download resuming. Super interesting.

0

There are 0 best solutions below