AVAssetDownloadTask doesn't start on IOS 10.3 device

814 views Asked by At

I'm actually implementing a download functionality in an application. I'm facing a very weird bug with AVAssetDownloadTask.

Indeed at the beginning of the day the implementation for downloading a asset was working.

When I called the resume function on my AssetDownloadTask, newly created, the download started instantly and the AVAssetDownloadDelegate function

func urlSession(
       _ session: URLSession,
       assetDownloadTask: AVAssetDownloadTask,
       didLoad timeRange: CMTimeRange,
       totalTimeRangesLoaded loadedTimeRanges: [NSValue],
       timeRangeExpectedToLoad: CMTimeRange
)

was called and showed progress for the active downloading task.

But at one point in the day the process stopped working. Now when I try to start/resume a AssetDownloadTask nothing happens. No AVAssetDownloadDelegate function is called for the progression of the download. It is like nothing is happening. I don't even get an error.

The weird thing is if I cancelled this AssetDownloadTask the

func urlSession( _ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL )

and

func urlSession(
       _ session: URLSession,
      task: URLSessionTask,
      didCompleteWithError error: Error?
)

are correctly called showing that the process seems to be on hold somehow.

I don't seem to have any of this problems on devices that are running on IOS 11.

I was wondering if any of you had an idea that could explain why my AssetDownloadTask doesn't start/resume on IOS 10.3 devices.

You will find below a few parts of my download manager.

let backgroundConfiguration = URLSessionConfiguration.background(withIdentifier: "\(Bundle.main.bundleIdentifier!).background")
    // Create the AVAssetDownloadURLSession using the configuration.
assetDownloadURLSession = AVAssetDownloadURLSession(configuration: backgroundConfiguration, assetDownloadDelegate: self, delegateQueue: OperationQueue.main)
private var activeDownloadsMap = [AVAssetDownloadTask: XXXXXXX]()   

func downloadAsset(for asset: XXXXX) {

    // Get the default media selections for the asset's media selection groups.
    let preferredMediaSelection = asset.geturlAsset().preferredMediaSelection


    if #available(iOS 10.0, *) {
        guard let task = assetDownloadURLSession.makeAssetDownloadTask(asset: asset.geturlAsset(),
                                                                                                                                     assetTitle: asset.title,
                                                                                                                                     assetArtworkData: nil,
                                                                                                                                     options: nil) else { return }

        // To better track the AVAssetDownloadTask we set the taskDescription to something unique for our sample.
        task.taskDescription = asset.title

        activeDownloadsMap[task] = asset

        task.resume()
    } else {
        return
    }
}       


extension DownloadManager: AVAssetDownloadDelegate {
public func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didLoad timeRange: CMTimeRange, totalTimeRangesLoaded loadedTimeRanges: [NSValue], timeRangeExpectedToLoad: CMTimeRange) {
    // This delegate callback should be used to provide download progress for your AVAssetDownloadTask.
    guard let asset = activeDownloadsMap[assetDownloadTask] else { return }

    var percentComplete = 0.0
    for value in loadedTimeRanges {
        let loadedTimeRange: CMTimeRange = value.timeRangeValue
        percentComplete +=
            CMTimeGetSeconds(loadedTimeRange.duration) / CMTimeGetSeconds(timeRangeExpectedToLoad.duration)
    }

    debugPrint("DOWNLOAD: Progress \( assetDownloadTask) \(percentComplete)")


}
}

Thank you in advance for any help you could give me to try and figure this out. Cant figure out if it is coming from the device / code / OS version

Best regards,

Martin

0

There are 0 answers