【发布时间】:2026-01-03 03:45:01
【问题描述】:
自从 2 天以来,我感觉我正在搜索整个网络以解决多个 http 请求的问题。所以我的工作流程是这样的:
-
上传图片到服务器
- 响应 = 带有任务 ID 的 XML 格式
-
使用任务 ID 向服务器发出 GET 请求以检查此任务的状态。
- 响应 = XML 格式,其中状态可能是“已完成”、“进行中”、“已排队”
- 如果 Status != "Completed" - 重试第 2 步
- 如果状态 == “已完成” - 转到第 3 步
从resultUrl下载结果
我最后一次尝试是使用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
}
【问题讨论】:
标签: ios swift alamofire promisekit