In TCA: How to extract from Effect when not in the reducer?

I have the results from a publisher in an Effect<[Int], Error>
How do I assign that [Int] to a variable?

Although I am able to get the results using this:

         case .reviewed:
               return environment.networkQuery.reviewed(pageCount: 1)
                  .catchToEffect()
                  .map(BespokeAction.processQueryResults)
         case let .processQuestionResult(.success(ids)):
             ...
  
         case let .processQuestionResult(.failure(error)):
            print(error)
           ...

I want to use the publisher outside of the reducer:

let values: Effect<[Question], Error> = environment.networkQuery.reviewed(pageCount: 1)

what do I do to values to extract the array upon success?

Hi @Mozahler, the Effect type conforms to the Combine Publisher protocol, so you can use it in anyway you would typically use a publisher. This includes subscribing to it in order to get its values:

values.sink { questions in 
  ...
}

Note that sink returns a cancellable that you will have to hold onto in order for the publisher to remain alive to do its work.

Thank you!

Referencing instance method 'sink(receiveValue:)' on 'Publisher' requires the types 'Error' and 'Never' be equivalent. I know I've watched you solve this error in the videos, but I can't remember where.

If the publisher can error you must invoke a different .sink method:

values.sink( 
  receiveCompletion: { ... },
  receiveValue: { ... }
)

Problem solved! I had tried what you're recommending before creating a question and it didn't work. The problem was the code I had was in a function so my cancellable was disappearing when the function returned (but the publisher hadn't finished)
I moved the cancellable variable to the view and everything now works.
Obviously, I'm new to working with publishers. I'll have to go back and find some of your videos on the subject. :+1:

What can we do here now that Combine Publisher conformance is deprecated?

1 Like