3
votes

I need to send image to server and waiting server for response. The pictures are big and it takes a long time so I wanted to parallelize that. I tried many ways but i get error :

2019-12-20 15:01:49.148156+0100 Catch Photo[644:138111] Task <81527D6C-279F-4644-9F5A-DE00665BCBF2>.<18> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x282184240 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalUploadTask <81527D6C-279F-4644-9F5A-DE00665BCBF2>.<18>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalUploadTask <81527D6C-279F-4644-9F5A-DE00665BCBF2>.<18>" ), NSLocalizedDescription=The request timed out.

exteriorQueinAsinc.async {
   let queue = OperationQueue()
      for i in 0..<images.count
      {
          queue.addOperation {
          self.sendImageToServer(image: images[i], imageName: imageNames[i])
       }
   }
   queue.waitUntilAllOperationsAreFinished()
}

Send Image function:

 func SendImageToServer(image:UIImage,imageName:String){
       let userId = AppSettings.user.id
        let token = AppSettings.token

        let url = "http://intaiapi.catch-system.com/vehicle_image"
        guard let imageData = image.jpegData(compressionQuality: 1.0) else { return }

        let parametar : [String:String] = [
                        "apiUrl":apiUrl,
                        "userId":userId!,
                        "userToken":token,
                        ]

       let manager = Alamofire.SessionManager.default
       manager.session.configuration.timeoutIntervalForRequest = 1200
       manager.upload(multipartFormData: { multipleData in
           for (key, value) in parametar {
               multipleData.append(value.data(using: String.Encoding.utf8)!, withName: key)
           }
       multipleData.append(
           imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")}, to: url) {
               (result) in
        switch result {

                     case .success(let upload, _, _):
                         upload.responseJSON { response in
                             if let image = UIImage(data: response.data!) {
                                 handler(.success(image))
                             } else {
                                 NotificationCenter.default.post(name: .ClearImageErrorNotification, object: nil)
                                 handler(.failure(.NotValidStatusCode(response.debugDescription)))
                             }
                     }
                     case .failure(let encodingError):
                         NotificationCenter.default.post(name: .ClearImageErrorNotification, object: nil)
                         handler(.failure(.NotValidStatusCode(encodingError.localizedDescription)))
                         print(encodingError)
                     }
       }
}

One of the ways I tried is this one. I use Alamofire.

1
Request timed out error... show your send image code - Scriptable
Code for send image is added - Stefan
have you tried just uploading a single image to see if it actually works? also your 'task' doesn't let the queue know that it is finished, so will never know when it ends - Scriptable
For 3-4-5 images is work correct but for 7-8-9.... return error - Stefan
have you tried reducing the resolution for your images? or its necessary the real size? - Andres Gomez

1 Answers

1
votes

You can increase timeout for waiting request complete. Set

manager.session.configuration.timeoutIntervalForResource = 1200

or any other value near

manager.session.configuration.timeoutIntervalForRequest = 1200