iOS Development

ios – URLSession uploadTask not displaying progress

Spread the love


I am having a problem with displaying progress in a add process. I’ve created a neighborhood specific app to check it is conduct. However progress / delegate at all times throughs the identical outcomes. I am additionally conforming to URLSessionTaskDelegate.
That is the best way I configure URLSession it is at all times .default

 non-public lazy var session: URLSession = {
     .init(
        configuration: self.configuration,
        delegate: self,
        delegateQueue: OperationQueue.predominant
    )
}()
non-public var commentary: NSKeyValueObservation?

Then I name it utilizing this func and convert it right into a writer

func loadData1(path: String, technique: RequestMethod, params: [String: String]) -> AnyPublisher<Knowledge, Error> {
    let writer = PassthroughSubject<Knowledge, Error>()
    guard let imageUrl = Bundle.predominant.url(forResource: "G0056773", withExtension: "JPG"),
          let imageData = attempt? Knowledge(contentsOf: imageUrl) else { return Fail(error: NSError(area: "com.check.predominant", code: 1000)).eraseToAnyPublisher() }
    var request = URLRequest(url: URL(string: "http://localhost:3000/")!)
    request.httpMethod = "POST"
    let knowledge = request.createFileUploadBody(parameters: [:], boundary: UUID().uuidString, knowledge: imageData, mimeType: "picture/jpeg", fileName: "video")
    let process = session.uploadTask(with: request, from: knowledge) { knowledge, response, error in
        if let error = error {
            return writer.ship(completion: .failure(error))
        }
        guard let response = response as? HTTPURLResponse else { return writer.ship(completion: .failure(ServiceError.different)) }
        guard 200..<300 ~= response.statusCode else { return writer.ship(completion: .failure(ServiceError.different)) }
        guard let knowledge = knowledge else { return writer.ship(completion: .failure(ServiceError.customized("No knowledge"))) }
        
        return writer.ship(knowledge)
    }
    commentary = process.progress.observe(.fractionCompleted) { progress, _ in
        print("progress: ", progress.totalUnitCount)
        print("progress: ", progress.completedUnitCount)
        print("progress: ", progress.fractionCompleted)
        print("***********************************************")
    }
    process.resume()
    return writer.eraseToAnyPublisher()
}

request.createFileUploadBody is simply an extension of URLRequest that I’ve created to create the shape knowledge’s physique.

Delegate

    func urlSession(_ session: URLSession, process: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
    print(bytesSent)
    print(totalBytesSent)
    print(totalBytesExpectedToSend)
    print("*********************************************")
}

Regardless of how large or small is the file that I am importing I at all times get the identical outcomes.
The delegate is just referred to as as soon as. And the progress can be at all times the identical.

progress:  100
progress:  0
progress:  0.0095
***********************************************
progress:  100
progress:  0
progress:  4.18053577746524e-07
***********************************************
2272436
2272436
2272436
*********************************************
progress:  100
progress:  95
progress:  0.95
***********************************************
progress:  100
progress:  100
progress:  1.0
***********************************************

^^^^^^ Output from prints

Does anybody is aware of what may I be doing improper?
Thanks upfront

Leave a Reply

Your email address will not be published. Required fields are marked *