ios – Swift Combine Cancel Publishers Without AnyCancellable


I have my networking library based on Combine. Anywhere in my app I can make a request and the networking library returns a publisher, it doesn’t have access to the AnyCancellable that is created that actually triggers the pipeline. What I need is the ability to cancel all network requests when the use logs out. Is there a way to cancel Combine pipelines from the publisher not the AnyCancellable.

Here is an example:

    var subscribers = [AnyCancellable]()
    let url = URL(string:"")!
    let request: AnyPublisher<UIImage, URLError> = URLSession.shared.dataTaskPublisher(for: url)
        .compactMap { UIImage(data:$ }
        .sink(receiveCompletion: { _ in
            print("subscription2 completed")
        }, receiveValue: { image in
            print("subscription1 value: \(image.scale)")
        .store(in: &subscribers)
    // request.cancel()

I would like to call something like request.cancel() on the publisher so that the receiveValue is never triggered.