While the code I wrote works perfectly, a lot of it based on many different tutorials. Am now in the "cleaning up phase", so am wondering if I need section 2 in the following?
At the top of my UIViewController, (myMain) I have the following lines:
1- let uploadService = UploadService()
2- lazy var uploadSession: URLSession = {
let configuration = URLSessionConfiguration.default
return URLSession(configuration: configuration, delegate: self,
delegateQueue: .main)
}()
Later in the my code, when it's time to send a POST request I proceed from the "upload" button in myView:
uploadService.start(upFile: xFile!, script: "uploadOrig.pl", myView: self)
The UploadService is:
class UploadService {
var uploadSession = URLSession.shared
func start(upFile: XFile, script: String, myView: UploadInv) {
var request = upFile.makeUrlReq(upFile: upFile, script: script)
uploadSession.uploadTask(with: request, from: request.httpBody )
{ (data, response, error) in
if let response = response {
myView.upResp(resp: response) }
if let error = error {
myView.upErr(error: error) }
if let data = data {
myView.upData(data: data) }
}.resume()
}}
All the methods inside of myView that are called by UploadClass, are wrapped in :
DispatchQueue.main.async { [self] in
do { ... }
So my uncertainty is this:
The class that handles the uploading has its own completion handler, ergo I don't believe I need to set a delegate. As well, Swift runs it on a background thread automatically.
Everything works, whether I comment out section 2 above or not. But that doesn't mean that both ways are correct.
Must I set the delegate and delegateQueue? Or may I keep it commented out and allow the Upload?