Since 2 days it feels that I'm searching the whole web to solve my problem with multiple http requests. So my workflow looks like this:
Upload a image to a server
- Response = XML Format with a Task ID
GET request to the server with the Task ID to check the status of this task.
- Response = XML Format where the status could be "Completed", "In Progress", "Queued"
- If Status != "Completed" - retry step 2
- If Status == "Completed" - go to step 3
Download the result from the resultUrl
My last try was to use PromiseKit
to chain the requests in a clean way like described in this post: Chain multiple Alamofire requests. But I don't know how to loop the second step every 2-5 seconds if the status isn't completed yet.
Is there a recommended solution for this workflow? This was my test with PromiseKit
, where i successfully chained the requests without a loop:
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
}