0

我正在尝试使用后台模式上传视频,但我得到libc++abi.dylib: terminating with uncaught exception of type NSException了 uploadTask。

fileprivate final class MultiPartHandler: NSObject {
    typealias SuccessHandler = ((Data?, URLResponse?, Error?) -> Void)

    var progressHandler: ProgressHandler
    var successHandler: SuccessHandler
    var errorHandler: ErrorHandler

    var uploadProgress: Progress
    var trackingQueue: DispatchQueue
    let sessionId: String
    let mediaData: Data
    let urlRequest: URLRequest

    required init(_ sessionId: String,_ queue: DispatchQueue,_ urlRequest: URLRequest,_ mediaData: Data,_ progress: @escaping ProgressHandler,_ success: @escaping SuccessHandler ,_ failure: @escaping ErrorHandler) {
        self.sessionId = sessionId
        self.trackingQueue = queue
        self.progressHandler = progress
        self.successHandler = success
        self.errorHandler = failure
        self.urlRequest = urlRequest
        self.mediaData = mediaData
        self.uploadProgress = Progress(totalUnitCount: 0)
        super.init()
    }

    func upload() {
        let config = URLSessionConfiguration.background(withIdentifier: "background_url_task")
        let session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue())

        let uploadTask = session.uploadTask(with: urlRequest, from: mediaData, completionHandler: self.successHandler)
        uploadTask.resume()
    }
}

如果我将配置更改为URLSessionConfiguration.default可以正常工作,但它不适用于后台选项。

知道如何修复或调试这个吗?

4

0 回答 0