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