Alamofire uploadProgress never called

48 Views Asked by At

I have the following code where I upload documents, the upload itself working fine but uploadProgress never getting called

do {
    let _ = fileUrl.startAccessingSecurityScopedResource()
    let file = try Data(contentsOf: fileUrl)
    fileUrl.stopAccessingSecurityScopedResource()
                
    let pendingRequest = session.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(file, withName: "file", fileName: fileUrl.lastPathComponent, mimeType: fileUrl.pathExtension)
            multipartFormData.append(typeOfDocument.data(using: .utf8)!, withName: "fileType")
            multipartFormData.append(customerId.data(using: .utf8)!, withName: "customerId")
            multipartFormData.append(fileId.data(using: .utf8)!, withName: "fileId")
        },
        to: url,
        method: .post,
        headers: headers
    ).uploadProgress(closure: { progress in
        print(progress.fractionCompleted)
    }).responseString { response in
        guard let decodedModel = try? JSONDecoder().decode(UploadDocumentsResponse.self, from: response.data) else {
            completionHandler(.failure(.cannotDecode))
            return
        }
        
        completionHandler(.success(decodedModel))
    }
    return CancellableRequest(pendingRequest: pendingRequest)
} catch let error {
    fileUrl.stopAccessingSecurityScopedResource()
    print(error)
    return nil
}


final class CancellableRequest {
    
    /// A Boolean value stating whether a request is cancelled.
    var isCancelled: Bool {
        pendingRequest.isCancelled
    }

    private let pendingRequest: Request
    
    init(pendingRequest: Request) {
        self.pendingRequest = pendingRequest
    }
    
    /// Cancels the represented request.
    func cancel() {
        pendingRequest.cancel()
    }
}

I have tried looking for a solution but couldn't find anything, looks like I am missing something obvious but can't quite figure it out what, tried uploading large file, the same result

Also tried the uploadProgress(queue, closure) the same result

Here's my Session configuration

self.session = Session(
    configuration: .ephemeral,
    requestQueue: DispatchQueue(label: AppConfiguration.shared.bundleId!.appending(".MyAPI"), qos: .background),
    interceptor: APIRequestInterceptor(languageManager: languageManager)
)
1

There are 1 best solutions below

0
Abdurakhmon On

I found what is causing the problem, I am using wormholy library and apparently when using wormholy alamofire upload progress does not work, I removed wormholy and it started working