2

2 日以来、複数の http リクエストに関する問題を解決するために Web 全体を検索しているように感じます。したがって、私のワークフローは次のようになります。

  1. サーバーに画像をアップロードする

    • 応答 = タスク ID を含む XML 形式
  2. タスク ID を使用してサーバーに GET 要求を送信し、このタスクのステータスを確認します。

    • 応答 = ステータスが「完了」、「進行中」、「待機中」の XML 形式
    • Status != "Completed" の場合 - ステップ 2 を再試行します。
    • Status == "Completed" の場合 - ステップ 3 に進みます
  3. resultUrl から結果をダウンロードする

私の最後の試みはPromiseKit、この投稿で説明されているように、リクエストをきれいな方法で連鎖させるために使用することでした: Chain multiple Alamofire requests。しかし、ステータスがまだ完了していない場合、2〜5秒ごとに2番目のステップをループする方法がわかりません。

このワークフローに推奨されるソリューションはありますか? PromiseKitこれは、ループなしでリクエストを正常にチェーンした での私のテストでした:

let request = Client.imageUploadRequest(image: imageView.image!)
let httpOperation = HTTPOperation(withRequest: request)

httpOperation.sendRequest().then() { result -> Promise<String> in
    let xml = SWXMLHash.parse(result)
    let id = self.getXMLAttribute(from: xml, with: "id")!
    let taskStatusrequest =  Client.getTaskStatusRequest(withTaskID: id)
    let httpOperation = HTTPOperation(withRequest: taskStatusrequest)

    return httpOperation.sendRequest()
}
// Loop this result if status != "Completed"
.then { result -> Promise<Data> in
    let xml = SWXMLHash.parse(result)
    let downloadUrl = self.getXMLAttribute(from: xml, with: "resultUrl")!
    let downloadRequest = Client.getDownloadRequest(withUrl: downloadUrl)
    let httpOperation = HTTPOperation(withRequest: downloadRequest)

    // if status != "Completed" don't return, retry this step
    return httpOperation.downloadData()
}
.then { _ -> Void in
    // Refresh View with data
}
4

2 に答える 2