快速同步发送多个HTTP POST请求

时间:2018-11-23 15:09:58

标签: swift grand-central-dispatch

在我的代码中,我需要根据用户的选择向服务器发送多个HTTP POST命令(用户选择图片,每个命令包含一张图片)。

我需要先通过HTTP POST发送一个文档,然后通过HTTP POST发送图片,最后通过HTTP HTTP POST提交。当前它几乎可以正常工作,但是如果发送的HTTP POST命令太多//我有一些HTTP错误(例如,发送了16张图片,共21张图片)

因此,我想确保在成功发送前一个HTTP命令后,将每个HTTP命令一个一个地发送到服务器。

这是我正在使用的代码:

            self.sendDocument{ (response) in
            if let result = response as? Bool {
                if(result == true){
                    self.Documentsent = true

                    print("Document sent, now sending photos")
                    progressDownload.progress = 0.3
                    var i = 0;
                    var j = 0;
                    //now we are sending the Photos !
                    for it in selectedPictures.sharedInstance.selectedCells{

                        let fileName = PhotoGallery.sharedInstance.photoGallery[it.item].fileName
                        self.constatImage = self.getSavedImage(named: fileName!)!

                        self.semaphore.signal()
                        self.envoiPhoto(obj: PhotoGallery.sharedInstance.photoGallery[it.item], pic: self.constatImage, num: it.item){ (result) -> () in
                            print("Envoi Photo \(it.item): \(result)")

                            print("i: \(i), count: \(selectedPictures.sharedInstance.selectedCells.count)")
                            _ = self.semaphore.wait(timeout: DispatchTime.distantFuture)
                            if(i == selectedPictures.sharedInstance.selectedCells.count-1){
                                print("for loop all pictures are sent")
                                self.allPhotosSent = true
                                self.myGroup.leave()
                            }
                            i = i+1

                        }
                        if(progressDownload.progress != 0.8){
                            progressDownload.progress += 0.2
                        }

                        j = j+1;

                    }
                }
                    //Case when document sending is failed
                else{
                    self.Constatsent = false
                }

            }
        }

        self.myGroup.notify(queue: .main, execute: {
            print("in notify: we have sent all the pictures")
            if(self.Documentsent && self.allPhotosSent){
                alertController.dismiss(animated: true, completion: ({
                    if(Constat.sharedInstance.type == "4"){
                        sleep(2)
                        self.envoiCommit()
                    }
                    self.envoiSuccess()
                }))
            }
            else{
                print("error")
                alertController.dismiss(animated: true, completion: ({
                    self.envoiError()
                }))
            }

        })

当前我有以下行为:

  • 发送HTTP POST文档
  • HTTP POST文档完成后,将发送整个HTTP POST照片
  • 发送完所有HTTPO POST照片后,即发送了HTTP POST提交。

但是我想让HTTP POST照片一张一张地发送。

我该如何实现?

编辑1

我也试图像这样使用信号量和Dispatcher:

var i: Int = 0

    let dispatchGroup = DispatchGroup()
    let dispatchQueue = DispatchQueue(label: "test")
    let dispatchSemaphore = DispatchSemaphore(value: 0)

    DispatchQueue.main.async{
        while(i<50){
            dispatchGroup.enter()
                print("This is a synchronized closure iteration: \(i)")
            self.dummyHTTPPOST{ (response) in
                if let result = response as? Bool {
                    if(result == true){
                        print("HTTP POST Sent")
                        dispatchSemaphore.signal()
                        dispatchGroup.leave()
                    }}
            }
            print("round finished")
            dispatchSemaphore.wait()
            i = i+1;

        }
    }

但是从不发送dummyHTTPPOST()方法中的HTTP请求。...session.DataTask从未启动。

1 个答案:

答案 0 :(得分:0)

我终于找到了一种使用session.DataTask同步发送多个HTTP POST命令的解决方案。

这是解决方案:

        var i: Int = 0
    let dispatchSemaphore = DispatchSemaphore(value: 0)

    DispatchQueue.global(qos: .userInitiated).async{
        while(i<500){
            self.dispatchGroup.enter()
            print("-------------------------------------------------------------------------")
                print("This is a synchronized iteration: \(i)")
            self.dummyHTTPPOST{ (response) in
                if let result = response as? Bool {
                    if(result == true){
                        print("HTTP POST Sent")
                        dispatchSemaphore.signal()
                    }}
            }
            print("round finished")
            dispatchSemaphore.wait()
            if(i == 49){
                print("sent all HTTP Commands")
                self.dispatchGroup.leave()
            }
            i = i+1;

        }

        DispatchQueue.main.async {
            print("************************")
            print("everything has been sent in Dispatch Queue")
            print("************************")
        }
    }

希望它会帮助其他人。