gpt4 book ai didi

ios - urlSession :dataTask:didReceive:completionHandler not called in Xcode8 Swift3

转载 作者:行者123 更新时间:2023-12-01 18:42:57 25 4
gpt4 key购买 nike

我创建了一个背景URLSession使用以下代码的对象:

let identifier = /* some background identifier */
let config = URLSessionConfiguration.background(withIdentifier: identifier)
self.session = URLSession(configuration: config, delegate: self, delegateQueue: nil)

我还实现了以下 URLSessionDataDelegate委托(delegate)类中的方法:
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
// code goes here
}

public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
// code goes here
}

public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
// code goes here
}

我可以成功调用 URLSessionUploadTask从本次 session 使用以下内容:
let fileURl = /* some file url */
let request = /* some URLRequest */
let task = session.uploadTask(with: request, fromFile: fileUrl)
task.resume()

但由于某种原因,我只收到以下回调:
`urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)` 


`urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)`.

委托(delegate)方法:
`urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void)`

永远不会被调用。

这是 Xcode8 中的错误还是我错过了一些重要的东西?
谢谢您的帮助!

最佳答案

Apple SDK 直接引用此方法:

/*
* Messages related to the operation of a task that delivers data
* directly to the delegate.
*/
@protocol NSURLSessionDataDelegate <NSURLSessionTaskDelegate>
@optional
/* The task has received a response and no further messages will be
* received until the completion block is called. The disposition
* allows you to cancel a request or to turn a data task into a
* download task. This delegate message is optional - if you do not
* implement it, you can get the response as a property of the task.
*
* This method will not be called for background upload tasks (which cannot be converted to download tasks).
*/
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler;

编辑:为报价添加更多上下文

关于ios - urlSession :dataTask:didReceive:completionHandler not called in Xcode8 Swift3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40378758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com