0

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:

Alamofire slow response

Alamofire slows down on big json responses

Gilby
  • 29
  • 3
  • Is it `print("response arrived")` or the log in `onDidFinish` that get 4-8 sec delay after the progress reaches 1? Is your app doing "heavy stuff" on main thread meanwhile? – Larme Sep 01 '22 at 09:52
  • @Larme print("response arrived"). What is delayed is the execution of the callback closure 'responseJSON'. But see my answer i just created for the resolution. – Gilby Sep 01 '22 at 10:33

1 Answers1

0

As so many times - a solution presents itself right after asking for it. I managed to get a session with a responsible backend dev of the server i connected to. He confirmed that the delay occurs on the server, not the client.

So it has nothing to do with Alamofire.

Here is what happens: When i send out an upload request that contains an image, the server hands that image data over to an image processing library which creates the final, to be saved image resource and a thumbnail resource - and sends them to another server that stores the images. Only after this whole process, the api server will let go of my request.

I'm questioning that this image processing and storing needs to take 6-8 seconds - but this is another topic.

Stack Overflow I would have deleted this questions since the 'problem' has nothing to do with Alamofire, but it seems i can't. Please let me know if i should have handled this in another way.

Gilby
  • 29
  • 3