I need to perform a scenario with the following steps:
- To make a network call with some search parameters provided by the user
- Then, to parse its JSON response and create model entities
- Then, for each entity created and if it has an associated image URL, to call the corresponding service for downloading such image and to show it in a
UICollectionView
when download finishes.
I need to handle concurrent network calls when downloading the N images I'd need. User should have the possibility to cancel the current search flow and start a new one. In addition, I should take into account that the cell for a certain image has not been displayed yet or it has been dequeued. So, my question is: which would be the best approach for this?
- Perform the first network call (no concurrency) and the JSON response parsing directly using
URLSession
andURLSessionDownloadTask
, and usingOperation
andOperationQueue
for the subsequent steps. Use bothinvalidateAndCancel()
for theURLSession
andcancelAllOperations()
for theOperationQueue
if the user wants to cancel the current search. - Perform the complete process using
Operation
andOperationQueue
. - Any other and more appropriate approach?
The best approach would be to do either a sync or async call to make the first service call. Once completed, parse the json synchronously and then use lazy loading concept (async call) to load if any images are to be downloaded to display the images.