由於2天感覺我正在搜索整個網絡來解決我的問題與多個http請求。所以我的工作流程是這樣的:斯威夫特 - 多循環鏈式http請求
上傳圖像到服務器
- 響應=有任務ID
GET請求到服務器的任務ID XML格式檢查此任務的狀態。
- 響應=在狀態可能是 「已完成」, 「進行中」, 「排隊」
- 如果狀態= 「已完成」 的XML格式 - !重試第2步
- 如果狀態==「已完成「 - 去從resultUrl
我最後一次嘗試步驟3
下載的結果是使用PromiseKit
以一種乾淨的方式鏈接請求,如本文中所述:Chain multiple Alamofire requests。但是如果狀態還沒有完成,我不知道如何每2-5秒循環第二步。
是否有針對此工作流的推薦解決方案?這是我的測試與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
}
謝謝。這是我的案例的一個完美例子。現在我可以清理我的解決方法來取回乾淨的代碼! – kaaPe