PassthroughSubject is a Subject in Combine framework, it acts both as a publisher and a subscriber.
The PassthroughSubject allows elements to be published by calling its send() method. When PassthroughSubject receives a subscriber, it keeps a reference to this subscriber and forward all published elements to it.
Here's an example of how to use PassthroughSubject:
main.swift255 chars10 linesIn the example above we created an instance of PassthroughSubject for a String, indicating that it will never encounter any errors (hence the Never). We then added a subscriber by calling .sink method on passthroughsubect. Finally, we sent a string value to the subject by calling .send() method.
Whenever we send a value to a PassthroughSubject, it will immediately forward that value to all existing subscribers.
PassthroughSubject is very useful when you want to emit events that may come from a source outside of the Combine framework, such as user interactions or data received from the network.
It is important to note that PassthroughSubject must be treated as a class, and not a struct. Its lifecycle depends on external references kept alive to it.
gistlibby LogSnag