Future and Subject in Combine.

Tram Ho

  • Although Combine has focused on the concept that publisher will emit sequence value of emit over the timeline, they have provided some convenient and fully functional API so that users do not need to set up custom publisher from head.
  • For example, when we want Combine to support us with built-in API like ImageProcessor to handle the completion handle pattern in closure in operations asynchronously when image processing completes or fails:

  • Instead of rewriting ImageProcessor we can handle the new way introduced in Combine . Not only do we keep the above implement , we can still use Combine to handle the completion handle case by case even when we add new code :

1 / Future is introduced:

  • We will focus on using the Future type known in the Future/Promise pattern , a pattern very popular in programming. Combine give us closure promise so that we can recognize when the operation asynchronous completion and will automatically map Result in the event Publisher .
  • What’s really handy is that in the above case to completion handle closure old used Result is Input , which means we can use Future with simple setup in func process as old as follows:

  • We simply use a dedicated completion handle closure and manually pass the result to the promise :

  • With Future we need a strict implementation of basic reactive closure API reactive in Combine and the future is simply publisher , equivalent to that we can use the following:

  • However, Future in Combine can only emit a single result value and will be immediately fulfilled and released when the promise is called.

2 / Handling various types of Output values:

  • Going back to the closure ImageProcessor , if we use 2 closure here, one keeps track of progress update like the image being process and the other is called when the process completes:

  • First of all we need to add the ProgressEvent enum to specify the type for the Output when the Publisher is initialized by us:

  • My initial wish was how to update the Combine API using Future but now we will use promise closure multiple times to report update in completed events :

  • However, the above implementation still does not work as expected because we only get the first results that are update before the above process completes.

3 / Use Subject to send the value:

  • We can send more value as we did above using 2 subject be introduced in Combine PassthroughSubject or CurrentValueSubject . We will use PassthroughSubject so that we can pass a value to each subscriber without retaining any value .
  • We can use the subject so that the ImageProcessing update can work well for both progress tracking and completed event .

  • The rest of our work must update the above using func before the ProgressEvent is processed instead of using the UIImage instance like so:

  • Please note when using PassthroughSubject that each subscriber will only receive value when subscription is active
  • The transition between the subject really very simple when we use CurrentValueSubject with the value of current that we need to track:

Share the news now

Source : Viblo