CurrentValueRelay: a convenient Combine publisher.

Imagine you want to store an object (with a publisher) and update it based on some event. One way to accomplish this would be the following:

  1. Store the object and its publisher somewhere.
  2. Create a publisher that describes how to update the object.
  3. Create a subscription that connects the object and the publisher.
  4. Store the subscription somewhere.

A basic implementation may look like this:

This implementation is cumbersome, however, because it requires multiple pieces of code in different locations to only really accomplish one thing. So I made a custom publisher that does all of this:

It only requires one declaration:

Personally, I think this publisher makes the code a lot cleaner.