Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

349
Views
Combine flatMap returning no expected contextual result type

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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()
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!