Post

Replies

Boosts

Views

Activity

Cancel URLSession.shared.dataTask
My code allows you to upload an image while it is showing the upload progress, each time I press the "Upload" button. Every time I press it, the progress starts at 0% until it ends at 100%. The problem is if I press the "Cancel" button, it cancels the upload of the image, but when I try to upload another image, it uploads it correctly but it does not calculate the progress. class UploadImage{ private var task: URLSessionDataTask? private var progressTask: Task<AnyObject, Error>? public func uploadImage() { self.task = nil self.task = URLSession.shared.dataTask(with: request) { data, response, error in if let data = data { print(String(data: data, encoding: .utf8)!) } else { print(String(describing: error)) return } } self.task?.resume() } public func cancelUpload(){ //self.progressTask!.cancel() self.task?.cancel() } public func uploadAllFiles() { self.progressTask = Task.detached{ var task2: URLSessionDataTask? while(self.state == 0){ if(self.task != nil){ task2 = self.task if(task2!.countOfBytesSent != 0){ //This is the progress print(round((Double(100.0)/Double(task2!.countOfBytesExpectedToSend)) * Double(task2!.countOfBytesSent)*100)/100) } } } return 0 as AnyObject } self.uploadImage() } } My view in swiftUI private var upload var body: some View { Button(action: { upload = Upload() Task.detached{ upload.uploadAllFiles() } }) { Text("Upload") } Button(action: { upload.cancelUpload() }) { Text("Cancel") } }
2
0
1k
Dec ’21
Cancel URLSession.shared.dataTask
My code allows you to upload an image while it is showing the upload progress, each time I press the "Upload" button. Every time I press it, the progress starts at 0% until it ends at 100%. The problem is if I press the "Cancel" button, it cancels the upload of the image, but when I try to upload another image, it uploads it correctly but it does not calculate the progress. class UploadImage{ private var task: URLSessionDataTask? private var progressTask: Task<AnyObject, Error>? public func uploadImage() { self.task = nil self.task = URLSession.shared.dataTask(with: request) { data, response, error in if let data = data { print(String(data: data, encoding: .utf8)!) } else { print(String(describing: error)) return } } self.task?.resume() } public func cancelUpload(){ //self.progressTask!.cancel() self.task?.cancel() } public func uploadAllFiles() { self.progressTask = Task.detached{ var task2: URLSessionDataTask? while(self.state == 0){ if(self.task != nil){ task2 = self.task if(task2!.countOfBytesSent != 0){ //This is the progress print(round((Double(100.0)/Double(task2!.countOfBytesExpectedToSend)) * Double(task2!.countOfBytesSent)*100)/100) } } } return 0 as AnyObject } self.uploadImage() } } My view in swiftUI private var upload var body: some View { Button(action: { upload = Upload() Task.detached{ upload.uploadAllFiles() } }) { Text("Upload") } Button(action: { upload.cancelUpload() }) { Text("Cancel") } }
Replies
2
Boosts
0
Views
1k
Activity
Dec ’21