1

I'm working on an app that needs to be able to upload an array (can contain a mix of UIImage or a custom struct that contains a local file URL and some data). I've seen Operations and OperationQueues as a possible starting point but I don't know if I'm looking in the right direction.

Hopefully, this image can provide some clarity regarding what I need to accomplish. Basically, each ProgressViewController can have its own "uploadable" array and I also need to track the progress for each upload. I know I need to use the URLSessionDelegate methods for tracking progress, but are Operations, OperationQueues and a singleton "UploadManager" of sorts the way to go? Thanks for your help guys!

Immanuel
  • 70
  • 8
  • 1
    That is a big big question with several answers. I'll try to point you in the right direction. You can use `NSOperations` as you mentioned before to handle the queue of the upload requests (you queue should be concurrent), each upload request should run in background so you need to use `URLSession` with a background configuration to be able to run in background. You need to use the `URLSessionDelegate ` to calculate the bytes uploaded and notify the progress. For a good talk about background, sessions take a look https://academy.realm.io/posts/gwendolyn-weston-ios-background-networking/. – Victor Sigler May 08 '18 at 03:24
  • 1
    If you decide to use `NSOperations` encapsulate your `Operation` separately to handle the upload tasks inheriting from `Operation` and provide the default. Take a look here https://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift – Victor Sigler May 08 '18 at 03:28
  • @VictorSigler Thanks Victor for the resources, they'll prove invaluable for sure. Can I ask you what you think about the idea of a singleton UploadManager class that keeps track of all the concurrent background uploads? – Immanuel May 08 '18 at 03:49
  • @VictorSigler Also, I'm wondering, should the queue be concurrent? I'm thinking it might be a better idea if it was a serial queue, and would that be a problem? Regarding URLSessions, considering that each ProgressViewController could potentially have different upload progress, I'm thinking I'd need to create a URLSession with a different background identifier for each ProgressViewController. Would that be right? – Immanuel May 08 '18 at 03:59
  • About your first question regarding the singleton, you should avoid it as far as you can. Remember singletons are mutable shared state across your app that at the end can be very difficult to tests. You can use services or inject the `UploadManager` if this is not necessary across your app. John Sundell has covered very well this in his blog. – Victor Sigler May 08 '18 at 04:05
  • About your second question, it's up to you. But I think the good solution should be a concurrent queue with a maximum of concurrent tasks to run. In your `UploadOperation` you can upload your file using `URLSession` with different identifiers and enqueue in the queue to run later the `OperationQueue` when all the operations are enqueued. – Victor Sigler May 08 '18 at 04:11
  • 1
    @VictorSigler Thanks Victor, I have some idea of what to do now and will try implementing something. I'll keep this post updated in case you're interested in how it goes. – Immanuel May 08 '18 at 05:24

0 Answers0