Proper way to detect user cancelled downloads using AVAssetDownloadDelegate

Hey all, wondering what the best way to detect a user cancelled download using the AVAssetDownloadDelegate. We're currently using the method:

public func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL)
    {
        if let error = assetDownloadTask.error as NSError?
        {
            switch (error.domain, error.code)
            {
                case (NSURLErrorDomain, NSURLErrorCancelled):

                    do
                    {
                        try FileManager.default.removeItem(at: location)
                    }
                    catch
                    {
                        DDLogError("An error occured trying to delete the contents on disk for \(error)")
                    }
                   
                    // if we do have a cancelled download and we have removed it, return before we save the location
                    return
                default:
                    DDLogError("An unexpected error occured \(error.domain)")
            }
        }
       
        // we need to save the location where the download finished, so when we call our delegate in
        // urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
        // we have a reference to it in order to pass along
        self.locationDict[assetDownloadTask.urlAsset.url] = location
    }

But have been experiencing a unconditionallyBridgeFromObjectiveC(_:) crash in Crashlytics with the following crash log

Crashed: com.-.ios.application.-.AssetDownloadUrlSession
0  libswiftFoundation.dylib       0x10270201c specialized static URL._unconditionallyBridgeFromObjectiveC(_:) + 8344
1  Networking                  0x10133dbc4 $S13Networking26AssetDownloadUrlSessionC03urlE0_05assetC4Task22didFinishDownloadingToySo12NSURLSessionC_So07AVAssetcH0C10Foundation3URLVtFTf4dnnn_n + 92
2  Networking                  0x1013394d0 $S13Networking26AssetDownloadUrlSessionC03urlE0_05assetC4Task22didFinishDownloadingToySo12NSURLSessionC_So07AVAssetcH0C10Foundation3URLVtFTo + 88
3  CFNetwork                      0x1b3f0dc78 __89-[NSURLSession delegate_AVAssetDownloadTask:didFinishDownloadingToURL:completionHandler:]_block_invoke + 36
4  Foundation                     0x1b42008bc __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
5  Foundation                     0x1b4108ab8 -[NSBlockOperation main] + 72
6  Foundation                     0x1b4107f8c -[__NSOperationInternal _start:] + 740
7  Foundation                     0x1b4202790 __NSOQSchedule_f + 272
8  libdispatch.dylib              0x1b31a86c8 _dispatch_call_block_and_release + 24
9  libdispatch.dylib              0x1b31a9484 _dispatch_client_callout + 16
10 libdispatch.dylib              0x1b314c82c _dispatch_continuation_pop$VARIANT$mp + 412
11 libdispatch.dylib              0x1b314bef4 _dispatch_async_redirect_invoke + 600
12 libdispatch.dylib              0x1b3158a18 _dispatch_root_queue_drain + 376
13 libdispatch.dylib              0x1b31592c0 _dispatch_worker_thread2 + 128
14 libsystem_pthread.dylib        0x1b338c17c _pthread_wqthread + 472
15 libsystem_pthread.dylib        0x1b338ecec start_wqthread + 4

Replies

So I have attempted to change the code up slightly to detect user cancellation and am still seeing the crash. Anyone have any suggestions?




   public func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL)
    {
        if let errorCode = assetDownloadTask.error?._code,
            errorCode == NSURLErrorCancelled
        {
            do
            {
                try FileManager.default.removeItem(at: location)
            }
            catch
            {
                DDLogError("An error occured trying to delete the contents on disk for \(error)")
            }

           
            // if we do have a cancelled download and we have removed it, return before we save the location
            return
        }
       
        // we need to save the location where the download finished, so when we call our delegate in
        // urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
        // we have a reference to it in order to pass along
        self.locationDict[assetDownloadTask.urlAsset.url] = location
    }



Crashed: com.-.ios.application.AssetDownloadUrlSession
0  libswiftFoundation.dylib       0x103afa01c specialized static URL._unconditionallyBridgeFromObjectiveC(_:) + 8344
1  Networking                     0x1027d2178 $S13Networking26AssetDownloadUrlSessionC03urlE0_05assetC4Task22didFinishDownloadingToySo12NSURLSessionC_So07AVAssetcH0C10Foundation3URLVtFTf4dnnn_n + 456
2  Networking                     0x1027ce3e8 $S13Networking26AssetDownloadUrlSessionC03urlE0_05assetC4Task22didFinishDownloadingToySo12NSURLSessionC_So07AVAssetcH0C10Foundation3URLVtFTo + 88
3  CFNetwork                      0x1dc049c78 __89-[NSURLSession delegate_AVAssetDownloadTask:didFinishDownloadingToURL:completionHandler:]_block_invoke + 36
4  Foundation                     0x1dc33c8bc __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
5  Foundation                     0x1dc244ab8 -[NSBlockOperation main] + 72
6  Foundation                     0x1dc243f8c -[__NSOperationInternal _start:] + 740
7  Foundation                     0x1dc33e790 __NSOQSchedule_f + 272
8  libdispatch.dylib              0x1db2e56c8 _dispatch_call_block_and_release + 24
9  libdispatch.dylib              0x1db2e6484 _dispatch_client_callout + 16
10 libdispatch.dylib              0x1db28982c _dispatch_continuation_pop$VARIANT$mp + 412
11 libdispatch.dylib              0x1db288ef4 _dispatch_async_redirect_invoke + 600
12 libdispatch.dylib              0x1db295a18 _dispatch_root_queue_drain + 376
13 libdispatch.dylib              0x1db2962c0 _dispatch_worker_thread2 + 128
14 libsystem_pthread.dylib        0x1db4c917c _pthread_wqthread + 472
15 libsystem_pthread.dylib        0x1db4cbcec start_wqthread + 4