Using Combine publishers in the reducer

Hi, I need to run a publisher function, returning AnyPublisher<Void, Never> from inside a reducer. How to do it properly?

For example,

    func requestData() -> AnyPublisher<Void, Never> {
        Deferred<PassthroughSubject<Void, Never>> { 
            do stuff
        }.eraseToAnyPublisher()
    }

This data request triggers changes in the "outside world" (external repository, which is not in the scope of the TCA Store/State). Before loading a view I need to trigger that data request and then listen to another publisher, which result is returned as an Effect in the same reducer.

Reducer looks something like this:

case .getData:
    return .merge(
        environment
            .repository
            .requestData(),
        environment
            .repository
            .dataPublisher
            .map(PresentAndLoadAction.updateData)
            .eraseToEffect()
)

Thanks!

If requestData is a publisher that just needs to do work in the outside world but not feed anything back into the system, you can fireAndForget it when you use it in the reducer:

case .getData:
  return .merge(
    environment.repository.requestData()
      .fireAndForget(),
    ...
  )
1 Like