iOS Swift - 崩溃:com.apple.NSURLSession-delegate EXC_BREAKPOINT 0x0000000102e35fc0

new*_*ion 7 thread-safety ios swift

我们的应用程序具有 pdf 和音频文件的下载功能。我在后台模式下添加了下载功能,但应用程序有时会崩溃,并且无法重现该步骤。这是 firebase 崩溃报告。

Crashed: com.apple.NSURLSession-delegate
0  ios                  0x102e35fc0 specialized DownloadSessionDelegate.urlSession(_:dataTask:didReceive:completionHandler:) + 189 (<compiler-generated>:189)
1  ios                  0x102e32780 @objc DownloadSessionDelegate.urlSession(_:dataTask:didReceive:completionHandler:) + 4339345280 (<compiler-generated>:4339345280)
2  CFNetwork                      0x1810db158 _CFNetworkHTTPConnectionCacheSetLimit + 155124
3  libdispatch.dylib              0x180573298 _dispatch_call_block_and_release + 24
4  libdispatch.dylib              0x180574280 _dispatch_client_callout + 16
5  libdispatch.dylib              0x18051cdcc _dispatch_lane_serial_drain$VARIANT$mp + 612
6  libdispatch.dylib              0x18051d8d8 _dispatch_lane_invoke$VARIANT$mp + 472
7  libdispatch.dylib              0x180527338 _dispatch_workloop_worker_thread + 712
8  libsystem_pthread.dylib        0x1c83bf5a4 _pthread_wqthread + 272
9  libsystem_pthread.dylib        0x1c83c2874 start_wqthread + 8
Run Code Online (Sandbox Code Playgroud)

我在 AppDelegate.swift 中添加了后台会话处理程序。

    //MARK: background session handling
    func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        print("-- handleEventsForBackgroundURLSession --")
        let backgroundConfiguration = URLSessionConfiguration.background(withIdentifier: identifier)
        let backgroundSession = URLSession(configuration: backgroundConfiguration, delegate: DownloadSessionDelegate.sharedInstance, delegateQueue: nil)
        print("Rejoining session \(backgroundSession)")
        
        DownloadSessionDelegate.sharedInstance.addCompletionHandler(completionHandler, identifier: identifier)
    }
Run Code Online (Sandbox Code Playgroud)

这是 DownloadSessionDelegate.swift 中的完成处理程序。我不确定问题是什么。

    //MARK: completion handler
    func addCompletionHandler(_ handler: @escaping CompleteHandlerBlock, identifier: String) {
        
        if identifier.isEmpty { return }
        
        DispatchQueue.global().async {
            self.handlerQueue[identifier] = handler
        }
        
    }
Run Code Online (Sandbox Code Playgroud)