How to cache the result of a Publisher that performs a network request

Hi everyone,

While transforming a task that performs a network request into a Combine Publisher, I came across the .share() and .multicast(_:) operators that offer the possibility to share the same stream, in this case a network request, across multiple subscribers thus avoiding new subscriptions to trigger a new network request while there's already one in progress.

These, anyway, only work while the request is in progress, after the request is finished any new subscription triggers a new request.

Does any of you know of a way to make this cacheable so that any new subscription after the first network request finished just gets the result without triggering a new request?
Maybe with a cache expiration time, too?

I think this could be a nice way for implementing getting and renewing an authentication token, for example.

Here's some sample code of how these operators can be used.
Code Block swift
let pipelineFork = PassthroughSubject<Data, Error>()
let expensivePublisher = somepublisher
.share()
.multicast(subject: pipelineFork)

Thank you very much and have a great WWDC!
How to cache the result of a Publisher that performs a network request
 
 
Q