Coordinating multiple HTTP requests with Combine framework
A common challenge for mobile development is dealing with multiple API endpoints that deliver heterogeneous data that you’d rather process as one single dataset.
This may arise for many reasons including
- you don’t want to proceed unless you have all the data
- it will be harder to keep local database integrity if you process each response data individually
I would normally use a DispatchGroup to achieve this. Launch multiple requests and use the semaphore controls to wait until all the responses have come back.
For example…
Not too bad if we’re just waiting for two requests, but the mess of juggling of responses and errors does not scale nicely when we inevitably end up with five or six endpoints that we need to coordinate.
One of the highlights at WWDC 2019 was the introduction of the Combine framework. The session Advanced Networking 1 session at WWDC2019 session starts with…
Networking is inherently asynchronous, that’s why it’s perfect to adopt Combine.
Can we use Combine for this problem?
Coordinating multiple requests with Combine
The Combine extension to URLSession is particularly neat as it handles the error cases nicely.
A simple zip()
does the trick!
This method scales well when you have many requests to coordinate.
Of course, you could also just sort out your backend APIs… 😀