Should I use operation queue for this complete scenario?

115 views Asked by At

I need to perform a scenario with the following steps:

  1. To make a network call with some search parameters provided by the user
  2. Then, to parse its JSON response and create model entities
  3. 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 and URLSessionDownloadTask, and using Operation and OperationQueue for the subsequent steps. Use both invalidateAndCancel() for the URLSession and cancelAllOperations() for the OperationQueue if the user wants to cancel the current search.
  • Perform the complete process using Operation and OperationQueue.
  • Any other and more appropriate approach?
2

There are 2 answers

4
NKB On

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.

0
Sean Lintern On

Perform the network call then make the model objects. return these to your VC through your API completion block.

Then reload your collection view and pass the url to each cell and have each cell worry about itself, use something like SDWebImage to show the images async in the cells..