I was blocked combine after reduce return value to down pipe line could not get map value

$offset
.debounce(for: .seconds(0.8), scheduler: DispatchQueue.main)
.tryMap { (offset) → AnyPublisher<ApprovalService.ApprovalQueryResult, Error> in

            return self.approvalService.getAssignedApprovals(count: AssignedApprovalsByRequestViewModel.increment, offset: offset)
        }
        .switchToLatest()
        .catch { [weak self] (error: Error) -> Empty<ApprovalService.ApprovalQueryResult, Never> in
            self?.assignedApprovalListError = error
            return Empty<ApprovalService.ApprovalQueryResult, Never>()
        }
        .map {
            self.totalCount = $0.totalCount
            return $0.result.publisher
        }
        .switchToLatest()
        .reduce([Request]()) { (parentRequests: [Request], approvalToSort: Approval) in
            
            guard let request = approvalToSort.request else {
                return parentRequests
            }
            request.approvals = [approvalToSort]
                return parentRequests + [request]
        }
        .**map { unwrappedArray -> [Request]? in**

** return unwrappedArray**
** }**
.assign(to: .requests, on: self)
.store(in: &disposables)

Hi @sivak

I’m not sure how to answer your post, to be honest. Maybe you can share more context so that our community can help you more.

For me, here’s my thoughts about what you’ve shared.

It seems like there is an issue with the code snippet provided after the reduce operator. The problem is that the reduce operator returns a single value of type [Request] , but the following map operator is expecting a publisher of type [Request]?.

To fix this issue, you can change the return type of the reduce operator to AnyPublisher<[Request], Never> and then use the eraseToAnyPublisher() method to convert it to AnyPublisher<[Request]?, Never> before using the map operator.

Here is the updated code:

.reduce([Request](), into: AnyPublisher<[Request], Never>.self) { 
(parentRequests: [Request], approvalToSort: Approval) in
            
            guard let request = approvalToSort.request else {
                return parentRequests
            }
            request.approvals = [approvalToSort]
                return parentRequests + [request]
        }
        .eraseToAnyPublisher()
        .map { unwrappedArray -> [Request]? in
            return unwrappedArray
        }

By doing this, the output of reduce will be converted to AnyPublisher and will have the same output as expected by the next operator(map) .

It’s also a good practice to check if the output of the reduce function is as expected, for example by adding a sink or a print statement to debug the output.

Thanks for your reply but code not working