0

In my use case, I have a list and with every cell, there is a button to download a particular document. From the listing, they can click on any number of those buttons and the document has to be downloaded in the background but in a serial fashion. We will have to show some animation while downloading a particular cell document.

I've tried concurrent way and it's working fine but our main application is not able to handle such requests. So, we need to call those APIs sequential. Please let me know how we can do that.

Any help will be highly appreciated.

RTXGamer
  • 3,215
  • 6
  • 20
  • 29
Rahul
  • 322
  • 1
  • 5
  • 18
  • Checkout - https://github.com/Heikowi/HWIFileDownload – RTXGamer Jul 17 '21 at 11:45
  • @RTXGamer: Thanks for sharing but looks like it's making a parallel call I need sequential calls. – Rahul Jul 17 '21 at 11:47
  • then create operations and add it in NSOperationQueue – RTXGamer Jul 17 '21 at 11:50
  • 2
    Create custom NSOperation with callback upon download that notifies the cell about the download state, add those operations into `NSOperationQueue` with `maxConcurrentOperationCount=1`. – Eugene Dudnyk Jul 17 '21 at 11:52
  • In particular see https://developer.apple.com/videos/play/wwdc2015/226/ which goes deeply into the pattern Eugene mentions. – Rob Napier Jul 17 '21 at 19:03
  • @EugeneDudnyk I tried maxConcurrentOperationCount but wasn't working. – Rahul Jul 21 '21 at 14:06
  • Out of curiosity, why do you need to download serially? That can make it much slower. It’s generally better to handle the concurrency properly rather than forcing it to run serially. Don't fight the concurrent nature of networking, but rather work with it. – Rob Jul 23 '21 at 15:03
  • Also, when you say “in the background”, you are talking about making sure it continues to download even if the user leaves the app? E.g. a background `NSURLSessionConfiguration`? If so, notions of operation queues, or the like, are a complete non-starter. – Rob Jul 23 '21 at 15:04
  • 1
    @RobNapier - Readers should be aware that that “advanced operations” subclass of theirs has problems. It is not compatible with new Foundation class names and there were subtle minor little bugs buried in the code (discussed over in Apple’s forums). Apple has (rightly, IMHO) abandoned this `AdvancedOperation` subclass, going so far as to remove the links to the code. Conceptually it was a lovely idea, and the general observations referenced in the video are good, but their “advanced operation” implementation was problematic. – Rob Jul 23 '21 at 20:49
  • @Rob The software and utility we are using to download it works with sequential download only. – Rahul Jul 26 '21 at 14:20
  • @Rob If the user leaves the app then it's fine if the file is not downloaded. – Rahul Jul 26 '21 at 14:21
  • That’s too bad that your tool constrains you so badly, but it is what it is. Yep, then wrapping the download in an operation is a fine solution, and add it to a serial queue, e.g. https://stackoverflow.com/a/32322851/1271826. – Rob Jul 26 '21 at 15:35
  • @Rob I will try this solution. Thanks, Rob for helping me out. – Rahul Jul 26 '21 at 15:51
  • Here is an Objective-C example of an asynchronous `NSOperation` custom subclass wrapping a `NSURLSessionDataTask`. https://stackoverflow.com/a/21205992/1271826. The `NSURLSessionDownloadTask` could follow a similar pattern. – Rob Jul 26 '21 at 16:10

0 Answers0