Hello,
I have a strange issue when deleting partially downloaded HLS streams. I'm using the HLSCatalog sample as a reference and the code is confusing.
Here's a snippet:
/// Tells the delegate that the task finished transferring data.
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
let userDefaults = UserDefaults.standard
/*
This is the ideal place to begin downloading additional media selections
once the asset itself has finished downloading.
*/
guard let task = task as? AVAggregateAssetDownloadTask,
let asset = activeDownloadsMap.removeValue(forKey: task) else { return }
guard let downloadURL = willDownloadToUrlMap.removeValue(forKey: task) else { return }
// Prepare the basic userInfo dictionary that will be posted as part of our notification.
var userInfo = [String: Any]()
userInfo[Asset.Keys.name] = asset.stream.name
if let error = error as NSError? {
switch (error.domain, error.code) {
case (NSURLErrorDomain, NSURLErrorCancelled):
/*
This task was canceled, you should perform cleanup using the
URL saved from AVAssetDownloadDelegate.urlSession(_:assetDownloadTask:didFinishDownloadingTo:).
*/
guard let localFileLocation = localAssetForStream(withName: asset.stream.name)?.urlAsset.url else { return }
do {
try FileManager.default.removeItem(at: localFileLocation)
userDefaults.removeObject(forKey: asset.stream.name)
} catch {
print("An error occured trying to delete the contents on disk for \(asset.stream.name): \(error)")
}
userInfo[Asset.Keys.downloadState] = Asset.DownloadState.notDownloaded.rawValue
case (NSURLErrorDomain, NSURLErrorUnknown):
fatalError("Downloading HLS streams is not supported in the simulator.")
default:
fatalError("An unexpected error occured \(error.domain)")
}
} else {
do {
let bookmark = try downloadURL.bookmarkData()
userDefaults.set(bookmark, forKey: asset.stream.name)
} catch {
print("Failed to create bookmarkData for download URL.")
}
userInfo[Asset.Keys.downloadState] = Asset.DownloadState.downloaded.rawValue
userInfo[Asset.Keys.downloadSelectionDisplayName] = ""
}
NotificationCenter.default.post(name: .AssetDownloadStateChanged, object: nil, userInfo: userInfo)
}
If the task completes, we generate a bookmark:
let bookmark = try downloadURL.bookmarkData()
and we save it.
if I was to delete the stream then, it all works fine. I can go to my device's settings and I can't see it iPhone Storage. So all good.
However.... if I try to cancel the download
task?.cancel()
Is invoked and the didCompleteWithError
function is called with a cancellation error. At this point, we haven't generated a bookmark for the file, so this will always fail
guard let localFileLocation = localAssetForStream(withName: asset.stream.name)?.urlAsset.url else { return }
do {
try FileManager.default.removeItem(at: localFileLocation)
But strangely, even if I try to generate a bookmark from the url location where the asset, I will get a bookmark, FileManager won't throw any errors on delete, but the partial download still remains on the device.
let url = try URL(resolvingBookmarkData: localFileLocation,
bookmarkDataIsStale: &bookmarkDataIsStale)
try FileManager.default.removeItem(at: url)
Any ideas?