2 일 이후로 여러 웹 요청을 통해 문제를 해결하기 위해 전체 웹을 검색하고 있습니다. 그래서 내 워크 플로우는 다음과 같습니다스위프트 - 루프가있는 다중 체인 http 요청
가 서버
- 응답 = 작업 ID로 서버에 작업 ID
GET 요청과 XML 형식으로 이미지를 업로드 이 작업의 상태를 확인하십시오.
- 응답 = 상태는 "대기"
- "진행 중"상태 = "완료"가 "완료"할 수있는 XML 형식 -! 다시 시도 2 단계
- 상태 == "완료 한 경우 "- resultUrl
내 마지막 시도에서 3
다운로드에게 결과를 단계로 이동하는 것은를 사용하는 것이 었습니다은이 게시물에 설명 된 것처럼 깨끗한 방법으로 요청을 연결합니다 : 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