swiftcombineswift-composable-architecture

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?

note: this a more finely resolved take on a question I posted previously:

previously asked


Solution

  • Although the code was mostly good, I never saw the results.

    This is because the code is inside a function and cancellable was disappearing with the function returning. The publisher immediately stops when the cancellable goes out of scope.

    I moved results and returnValues into the view and now update a @State property in the receiveValue closure:

       @State var returnValues = [Question]()
       @State var results: AnyCancellable? = nil
    
        ....
    
       func runQuery() {
         let env = BespokeEnvironment(mainQueue: .main, networkQuery: NetworkQuestionRequestor())
          results = env.networkQuery.reviewedQuestionsQuery(pageCount: 1)
                .sink(
                   receiveCompletion: { print($0)},
                   receiveValue: { values in
                      returnValues.append(contentsOf: values)
                   })
       }