After calling .resume()
on an Alamofire upload request, the request is successful and in the .uploadProgress
closure i see that the data got transferred in ~1-2 seconds. But before the .responseJSON
closure gets triggered, several seconds (4-8) go by.
So the whole process takes over 4x longer than it should, according to the progress closure.
I did find a few questions on the same topic - but no useful explanations why that is or even better, how to prevent this long delay.
What causes this delay and how to prevent it?
Here is an example that produces the delay. No further threading/async stuff involved.
func makeUploadRequest(url: URL, data: UploadData, onDidFinish: @escaping (DataRequest, AttachmentData?) -> Void) -> DataRequest {
let request = AF.upload(multipartFormData: { formData in
formData.append(data.doc, withName: "doc", fileName: data.filename, mimeType: data.mimeType)
formData.append(self.UUID().data(using: .utf8)!, withName: "vendor_id")
formData.append(data.entityId.data(using: .utf8)!, withName: "entity_id")
formData.append(data.entityType.data(using: .utf8)!, withName: "entity_type")
formData.append(data.filename.data(using: .utf8)!, withName: "file_name")
}, to: url, method: .post, headers: getHeader())
request.responseJSON { response in
print("response arrived")
onDidFinish(request, self.processResponse(response))
}
request.uploadProgress { progress in
print("Upload progress: \(progress.fractionCompleted)")
}
return request
}
Here are some of the unfortunately not so useful search results: