[SOLVED] Combine flatMap returning no expected contextual result type

Issue

In HomeRepositoryImpl is getWorldWideData function which returns AnyPublisher<WorldwideResponseItem, ErrorType>

func getWorldwideData() -> AnyPublisher<WorldwideResponseItem, ErrorType> {
    let url = RestEndpoints.worldwideStats.endpoint()
    let publisher: AnyPublisher<WorldwideResponseItem, ErrorType> = RestManager().fetchData(url: url)
    
    return publisher
        .flatMap {
            let filteredCountries = Array($0.countries.sorted(by: {$0.totalConfirmed > $1.totalConfirmed}).prefix(3))
            $0.countries = filteredCountries
            return Just($0)
        }
}

FlatMap takes WorldResponseItem from publisher and WorldResponseItem has countries array property which I wanted to sort, so I created new variable called filteredCountries and changed it value to array with 3 sorted countries, and I changed WorldResponseItem countries property to filteredCountries and flatMap is returning Just with WorldResponseItem from publisher.

But I am getting No ‘flatMap’ candidates produce the expected contextual result type ‘AnyPublisher<WorldwideResponseItem, ErrorType>’ error, and when I add .eraseToAnyPublisher() to pipeline, I am getting Type of expression is ambiguous without more context

Solution

There are several issues in your flatMap. You need to call setFailureType and eraseToAnyPublisher on the Just inside the flatMap and then also call eraseToAnyPublisher on the flatMap itself.

You should also use named closure arguments when working with nested closures – you have a sorted(by:) nested inside flatMap.

func getWorldwideData() -> AnyPublisher<WorldwideResponseItem, ErrorType> {
    let url = RestEndpoints.worldwideStats.endpoint()
    let publisher: AnyPublisher<WorldwideResponseItem, ErrorType> = RestManager().fetchData(url: url)

    return publisher
        .flatMap { response -> AnyPublisher<WorldwideResponseItem, ErrorType> in
            let filteredCountries = Array(response.countries.sorted(by: { $0.totalConfirmed > $1.totalConfirmed }).prefix(3))
            response.countries = filteredCountries
            return Just(response).setFailureType(to: ErrorType.self).eraseToAnyPublisher()
        }
        .eraseToAnyPublisher()
}

However, you shouldn’t be using flatMap in the first place. You are just mapping an output to a different value, so you can simply use map, which simplifies the closure quite a lot.

func getWorldwideData() -> AnyPublisher<WorldwideResponseItem, ErrorType> {
    let url = RestEndpoints.worldwideStats.endpoint()
    let publisher: AnyPublisher<WorldwideResponseItem, ErrorType> = RestManager().fetchData(url: url)

    return publisher
        .map { response -> WorldwideResponseItem in
            let filteredCountries = Array(response.countries.sorted(by: { $0.totalConfirmed > $1.totalConfirmed }).prefix(3))
            response.countries = filteredCountries
            return response
        }
        .eraseToAnyPublisher()
}

Answered By – Dávid Pásztor

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.