Sitemap
Jon Andersen

Jon Andersen

RX Progress

2 min readMar 13, 2016

Recently I needed to upload some files to a server. I needed to have a progress reported and wanted to make it in a clean RX manner. I explored a few options on how to implement this. The user sprynmr seemed to have encountered the same issue as I. The suggest approach can be found here

  1. Return a single signal that sends tuples.
  2. Return two signals.
  3. Create a class that represents the download process.

Return a single signal that sends tuples

I found it easier to represent the result as an Either, with some simplications to avoid having to specify the progress type.

To download the AVAsset of a video I would do the following

Get Jon Andersen’s stories in your inbox

Join Medium for free to get updates from this writer.

The consumer of this function will now be able to subscribe and recieve events that’s either progress or a final result.

For the onNext method I used the following extension

Wrapping the Events like this removes the need to pass an extra function to handle progress. But it falls short when the result needs more transformations. Doing simple operations such as map will yield code that’s much harder to work with.

The issue is the switch statement in the map function. We can implement some basic Map & FlatMap functions on our ResultProgress to reduce unwrapping we have to do

The new mapping function for the SignalProducer would look like this

Much easier to work with, but I don’t like how you need to call map again inside the map function. I haven’t found a good way to extend the SignalProducers map function to also map the ResultProgress. Have you found a better way to solve this?

--

--