我是否已将上传或下载任务放到后台线程中,还是 Swift 为我完成?
Do I have place upload or download tasks onto a background thread or does Swift do it for me?
我不确定 Swift 中用于 HTTP 方法的 thread/queue 控件。
在我的应用程序中从第 3 方服务器发出 GET 请求以及 POST 和其他请求。
在我的主 UIViewController 中,我用以下方法初始化 class:
let uploadService = UploadService()
lazy var uploadSession: URLSession = {
let configuration = URLSessionConfiguration.default
return URLSession(configuration: configuration, delegate: self, delegateQueue: .main)
}()
稍后我从同一个 UIViewController 调用它,let uploadService = UploadService()
在我开始向下面的 class 添加更多功能之前,我想问一下:
return URLSession(configuration: configuration, delegate: self, delegateQueue: .main)
当 UploadService class 向我的 UIViewController 调用方法时,Xcode 警告我从主 thread/queue 执行此操作,我这样做了。所以我的问题是,Swift 是否会自动移动任何与非主 thread/queue 相关的 HTTP,所以我不必担心它?因为如果是这样的话,那太好了。但是为什么要我把它放在主要 thread/queue 中,就像我在上面所做的那样? (这是我在教程中做的)。
只是想说清楚,我应该将其声明为背景 thread/queue,还是 Swift 为我处理它,而不管我在上面如何声明它?
class UploadService {
var uploadSession = URLSession.shared
func start(upFile: XFile, script: String, upLoadInvoiceClass: UploadInvoice) {
var request = upFile.makeUrlReq(upFile: upFile, script: script)
uploadSession.uploadTask(with: request, from: request.httpBody )
{ (data, response, error) in
if let response = response {
upLoadoiceClass.upResp(resp: response)
}
if let error = error {
upLoadoiceClass.upErr(error: error)
}
if let data = data {
upLoadoiceClass.upData(data: data)
}
}.resume()
}
}
URLSession 总是在后台线程上进行网络交互。你不必担心。
默认情况下,完成 handlers/delegate 方法也在后台线程上 运行。这使您可以在不占用主线程的情况下对结果数据进行 time-consuming 处理。
除非您在创建 URL 会话时指定一个不同的委托队列,否则您应该将您放入完成 handlers/delegate 方法中的任何 UI 代码包装在对主线程。
或者,您可以创建一个 URLSession 并在初始化程序中传递的 delegateQueue
参数中为其提供一个前台队列。如果你这样做,那么你的完成 handlers/delegate 方法将在该前台队列上 运行 (因此在主线程上 运行 )。如果你这样做,你不需要显式换行UIKit 调用主线程,但如果您在 URLSession 完成 handlers/delegate 方法中 time-consuming 工作,您将停止 UI =11=]
我不确定 Swift 中用于 HTTP 方法的 thread/queue 控件。
在我的应用程序中从第 3 方服务器发出 GET 请求以及 POST 和其他请求。
在我的主 UIViewController 中,我用以下方法初始化 class:
let uploadService = UploadService()
lazy var uploadSession: URLSession = {
let configuration = URLSessionConfiguration.default
return URLSession(configuration: configuration, delegate: self, delegateQueue: .main)
}()
稍后我从同一个 UIViewController 调用它,let uploadService = UploadService()
在我开始向下面的 class 添加更多功能之前,我想问一下:
return URLSession(configuration: configuration, delegate: self, delegateQueue: .main)
当 UploadService class 向我的 UIViewController 调用方法时,Xcode 警告我从主 thread/queue 执行此操作,我这样做了。所以我的问题是,Swift 是否会自动移动任何与非主 thread/queue 相关的 HTTP,所以我不必担心它?因为如果是这样的话,那太好了。但是为什么要我把它放在主要 thread/queue 中,就像我在上面所做的那样? (这是我在教程中做的)。
只是想说清楚,我应该将其声明为背景 thread/queue,还是 Swift 为我处理它,而不管我在上面如何声明它?
class UploadService {
var uploadSession = URLSession.shared
func start(upFile: XFile, script: String, upLoadInvoiceClass: UploadInvoice) {
var request = upFile.makeUrlReq(upFile: upFile, script: script)
uploadSession.uploadTask(with: request, from: request.httpBody )
{ (data, response, error) in
if let response = response {
upLoadoiceClass.upResp(resp: response)
}
if let error = error {
upLoadoiceClass.upErr(error: error)
}
if let data = data {
upLoadoiceClass.upData(data: data)
}
}.resume()
}
}
URLSession 总是在后台线程上进行网络交互。你不必担心。
默认情况下,完成 handlers/delegate 方法也在后台线程上 运行。这使您可以在不占用主线程的情况下对结果数据进行 time-consuming 处理。
除非您在创建 URL 会话时指定一个不同的委托队列,否则您应该将您放入完成 handlers/delegate 方法中的任何 UI 代码包装在对主线程。
或者,您可以创建一个 URLSession 并在初始化程序中传递的 delegateQueue
参数中为其提供一个前台队列。如果你这样做,那么你的完成 handlers/delegate 方法将在该前台队列上 运行 (因此在主线程上 运行 )。如果你这样做,你不需要显式换行UIKit 调用主线程,但如果您在 URLSession 完成 handlers/delegate 方法中 time-consuming 工作,您将停止 UI =11=]