`flatMap` publishers with different Errors

Viewed 77

I have a Swift publisher like the following

let item = PassthrogutSubject<String, Never>()

Upon receiving the String I want to make a network request

For that, I'm using flatMap

My network request signature looks like this:

func loadDetails(_ code: String) -> AnyPublisher<CustomType, ErrorAlert>

How can I do the following:

$item
 .filter { !$0.isEmpty }
 .flatMap { [unowned self] (string) in
    self.viewModel.loadDetails(string) }
 .receive(on: DispatchQueue.main)
 .sink(receiveCompletion: { _ in
     ...
  }) {  ... }
 .store(in: &subscriptions)

error:

Instance method 'flatMap(maxPublishers:_:)' requires the types 'Published<Value>.Publisher.Failure' (aka 'Never') and 'Alert' be equivalent

1 Answers

Halfway my question I solved it with the following operator on the receiving of the first subscriber

$item
 .filter { !$0.isEmpty }
 .setFailureType(to: ErrorAlert.self) // <----  HERE
 .flatMap { [unowned self] (string) in
    self.viewModel.loadDetails(string) }
 .receive(on: DispatchQueue.main)
 .sink(receiveCompletion: { _ in
     ...
  }) {  ... }
 .store(in: &subscriptions)
Related