I'm looking for a pattern to implement in my application that would allow me to cleanly have my application pull information from several http requests, where each request needs to run sequentially, one after the other, and feed information from the former request to the next request. I have worked through many of the solutions in this question, but many of the answers are for old versions of Swift, there are conflicting solutions, there are non-working solutions, and none address getting data from one request to the next request. Also many of the ideas don't extend to more than about two requests.
The reason for having several requests that do not overlap is that data from earlier requests feed into the next request. Or it could be that depending on the data, it might make sense to skip some http requests.
I have tried nesting requests, which works for one level deep, but does not work two or more levels deep. It also is hostile to logic where skipping some requests is required.
Alamofire.request(...).responseString { formResponse in
Alamofire.request(...).responseString { submitResponse in
Alamofire.request(...).responseString { getDataResponse in
// not only is this messy, it also did not proven to wait until all previous threads were done before working on the nested item.
}
}
}
I have also tried to set up a serial queue with the Alamofire-Synchronous package imported:
let serialQ = DispatchQueue(label: "serialQ")
var info1 = ""
var info2 = ""
var info3 = ""
serialQ.async {
Alamofire.request(...).responseString { formResponse in
....
info1 = "good stuff"
}
}
serialQ.async {
Alamofire.request(...).responseString { submitResponse in
....
if info1 == "x" {...}
info2 = "more good stuff"
}
}
serialQ.async {
Alamofire.request(...).responseString { getDataResponse in
....
if info2 == "x" {...}
info3 = "even more good stuff"
}
}
The above code acts as if that synchronous functionality of the package isn't doing anything to prevent Alamofire from spawning concurrent threads. In other words, it just cranks up all of the requests, one after the other. This might work, I didn't see where my code actually used the code in the package.
I also tried to use the idea presented in this SO answer, but I could not get it working. In fact I worked through all of the answers in that question, and none proved to work for me or some of the "chaining" solutions did not answer the question of how you feed data from one request to the next.
I must me making this harder than it has to be!
Making an http request, waiting for the result, and using it in the next http request, (and the next, and the next) has got to have an elegant swift / Alamofire programming pattern.
A good answer to this question would allow the reader to be able to construct code with the modern/current versions of Swift and Alamofire, that would to chain several http calls where the response from the former request is used in the next request. I've used 3 dependent requests in my examples, but should extend to "n" without getting bogged down.