title: How to implement a queue of serial network calls then processing in RxSwift?
body: <p>I'm working on an app where I want to achieve the following using RxSwift and RxCocoa</p>
  
  <ol>
  <li>Download JSON containing url to X number of files</li>
  <li>Download file 1, process file 1</li>
  <li>Download file 2, process file 2</li>
  <li>Download file 3, process file 3</li>
  </ol>
  
  <p>... etc</p>
  
  <p>The key here is that the processing of each file has to complete before downloading the next file. At the very least the order of the file processing must be performed in order. If I can start downloading file 2 while file 1 is processing, that would be awesome, but is not necessary.</p>
  
  <p>I've tried using SerialDispatchQueueScheduler to make this work, but I since the files are of different sizes, the download of each file finishes at different times, and therefore the processing code fires in a different order than I started the downloads.</p>
  
  <p>I could easily implement this without using Rx by using NSOperations and the like, but I'd like to keep using Rx in this app, as it's what I use elsewhere in this app.</p>
  
  <p>Below I've included a snippet with some of the code. Comments have been added for the sake of this question.</p>
  
  <pre><code>       .flatMap { [unowned self] (tasks: [DiffTask]) -&gt; Observable&lt;ApplyDiffStatus&gt; in
              return Observable.from(tasks)
                  .observeOn(self.backgroundScheduler) // StackOverflow: backgroundScheduler is a SerialDispatchQueueScheduler
                  .flatMapWithIndex({ [unowned self] (task, index) in
                      return self.fetchDiff(for: task, taskIndex: index, taskCount: tasks.count) // StackOverflow: Downloads a file from a URL
                  })
                  .catchError({ (error) -&gt; Observable&lt;DictionaryUpdater.DiffTaskProgress&gt; in
                      observable.onError(error)
                      throw error
                  })
                  .map({ (diffTask : DiffTaskProgress) -&gt; DiffTaskProgress.Progress in
                      // Stack Overflow: I've wrapped much of the progress observable in a Observable&lt;UpdateProgress&gt;
                      switch diffTask.progress {
                      case .started(currentTask: let currentTask, taskCount: let taskCount):
                          observable.on(.next(.fetchingDiff(progress: diffTask, currentDiff: currentTask, diffCount: taskCount)))
                      case .finished(data: _, currentTask: let currentTask, taskCount: let taskCount):
                          observable.on(.next(.fetchingDiff(progress: diffTask, currentDiff: currentTask, diffCount: taskCount)))
                      case .progress(completion: _, currentTask: let currentTask, taskCount: let taskCount):
                          observable.on(.next(.fetchingDiff(progress: diffTask, currentDiff: currentTask, diffCount: taskCount)))
                      }
  
                      return diffTask.progress
                  })
                  .flatMap({ [unowned self] (progress: DiffTaskProgress.Progress) -&gt; Observable&lt;ApplyDiffStatus&gt; in
                      switch progress {
                      case .finished(data: let data, currentTask: let currentTask, taskCount: let taskCount):
                          return self.applyDiff(data, currentTask: currentTask, taskCount: taskCount) // StackOverflow: PROCESSES THE FILE THAT WAS DOWNLOADED
                      default:
                          return Observable.empty()
                      }
                  })
          }
  </code></pre>