I'm looking for a way to prefetch data and cache it without any subscriptions made towards that subscriber or when first subscription is made.
For example :
products = getProducts.shareReply(scope : forever)
And when new subscribers subscribes it should receive the last data. If there is no subscribers it's shouldn't restart this publisher on next subscription instead it should return last cached data.
I'm already using shareReply operator with multicast, custom ReplaySubject and autoconnect but if there is no subscriptions towards the publisher next time something subscribes it will restart the publisher and new data will be fetched. And I don't want that.
This is code for shareReply operator :
extension Publisher {
func shareReplay(_ bufferSize: Int = 1) -> AnyPublisher<Output, Failure> {
return multicast(subject: ShareReplySubject(bufferSize))
.autoconnect()
.eraseToAnyPublisher()
}
}