Our application has the download function in pdf and audio files. I added the download function in background mode, but the application is crashed sometimes and I can't reproduce the step. Here is the firebase crash report.
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
I added the background session handler in 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)
}
And here is the completion handler in DownloadSessionDelegate.swift. I am not sure what the problem is.
//MARK: completion handler
func addCompletionHandler(_ handler: @escaping CompleteHandlerBlock, identifier: String) {
if identifier.isEmpty { return }
DispatchQueue.global().async {
self.handlerQueue[identifier] = handler
}
}