Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

346
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda