1

I have a Publisher

var subject = PassthroughSubject<Int, Error>()

I want to convert it to

PassthroughSubject<Int, Never>

Is there anyway to achieve this ?

Edit - More Details

I do not want the Publisher to complete and the linked answer did not work because catch still completes the publisher.

dev_ios999
  • 339
  • 3
  • 9
  • Why a `PassthroughSubject` specifically? Does an `AnyPublisher` not work for you? If it does, see [this](https://stackoverflow.com/questions/58227096/set-a-given-publishers-failure-type-to-never-in-combine). – Sweeper Sep 08 '21 at 00:44
  • Because my code is using PassthroughSubject but of course that can be changed. I have already tried this link. The problem is that the catch still requires another publisher. What i want is just to somehow ignore all the error and convert Error to Never so that it can be used downstream. – dev_ios999 Sep 08 '21 at 00:49
  • and additionally i do not want the publisher to complete – dev_ios999 Sep 08 '21 at 00:50
  • I have added more details. – dev_ios999 Sep 08 '21 at 01:00
  • Catch the error if one comes. Now you can turn the downstream type into whatever you like. https://www.apeth.com/UnderstandingCombine/operators/operatorsErrorHandlers/operatorscatch.html – matt Sep 08 '21 at 01:19
  • @matt From your link "The Output type of the publisher must match the Output type of the upstream pipeline." If i have understood it correctly that means cannot be converted to for downstream. However this is exactly what i want or am i misunderstanding. – dev_ios999 Sep 08 '21 at 01:24
  • I want to convert to so the downstream subscribers only see and the publisher continues publishing. – dev_ios999 Sep 08 '21 at 01:26
  • 1
    The output type is the same (`Int` in your example), not the error type. You may need to combine catch with `share` https://www.apeth.com/UnderstandingCombine/operators/operatorsErrorHandlers/operatorsretry.html – Paulw11 Sep 08 '21 at 01:54
  • I really don't know what you mean "cannot be converted to ``". That is exactly what you _can_ do. – matt Sep 08 '21 at 02:18

1 Answers1

2

Here's an example of using catch to turn an <Int, Error> into an <Int, Never>:

import UIKit
import Combine
class ViewController: UIViewController {
    let subject = PassthroughSubject<Int, Error>()
    var storage = Set<AnyCancellable>()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.subject
            .catch { what in
                Empty(completeImmediately:false)
            }
            .sink {print($0)}
            .store(in: &self.storage)
    }
}

If you send 1 to the passthrough subject now, you get 1 out the end. But if you send an error to the passthrough subject, no error arrives at the end; the pipeline end type is <Int, Never>.

However, do note that you can then never send another value. This has nothing to do with the pipeline; it's because once you send an error through a Subject, that Subject is dead. There's nothing you can do about that.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Hmm I thought that somehow there might be possibility that i can prevent the death of subject. This means that now i have redesign my subject. Probably need to add a dedicated subject for errors so that i do not have to send an error through a subject but instead sending error as an event. – dev_ios999 Sep 08 '21 at 09:12
  • Well that is what a Result is for. – matt Sep 08 '21 at 09:32