0
// get handle of native data task publisher
    let publisher = URLSession.shared.dataTaskPublisher(for: URL)
        .handleEvents(
            receiveSubscription: { _ in
                activityIndicatorPublisher.send(true)
            }, receiveCompletion: { _ in
                activityIndicatorPublisher.send(false)
            }, receiveCancel: {
                activityIndicatorPublisher.send(false)
            })
        .tryMap { data, response -> Data in
            guard let httpResponse = response as? HTTPURLResponse,
                  httpResponse.statusCode == 200 else {
                      throw NetworkError.httpError
                  }
            return data
        }
        .decode(type: Repository.self, decoder: JSONDecoder())
    .map { $0 }
        .catch { err in
            return Just([])
        }
        .eraseToAnyPublisher()
    return publisher

I am new to Combine and I can´t figure out what should I put inside the .map{} closure in order to return an array of Repository objects. The error I get at compile time is: Cannot convert value of type 'Repository' to closure result type '[Any]'

P.S. return type here should be:

-> AnyPublisher<[Repository], Never>

Can anyone share a light here? Many thanks in advance.

Fabrizio Prosperi
  • 1,398
  • 4
  • 18
  • 32
  • 1
    It’s clear from the code that Repository is a single object and not an array so I am not sure what you want to do. If you know it’s an array then change the decoding code to take an array – Joakim Danielson Jan 12 '22 at 08:32
  • Definitely, changed .decode(type: Repository.self, decoder: JSONDecoder()) to .decode(type: [Repository].self, decoder: JSONDecoder()) and it goes smoothly, thank you Joakim, that was a really dumb question indeed! – Fabrizio Prosperi Jan 12 '22 at 08:57

1 Answers1

0

It was a silly mistake on my side, as Joakim Danielson pointed out, it was enought to change return type to an array of Repositories and publisher was working fine, final version of it is the following:

 static func fetchRepositories(urlString: String) -> AnyPublisher<[Repository], Never> {
        // prepare URL
        guard let URL = URL(string: urlString) else  {
            return Just([]).eraseToAnyPublisher()
        }
        // get handle of native data task publisher
        let publisher = URLSession.shared.dataTaskPublisher(for: URL)
            .handleEvents(
                receiveSubscription: { _ in
                    activityIndicatorPublisher.send(true)
                }, receiveCompletion: { _ in
                    activityIndicatorPublisher.send(false)
                }, receiveCancel: {
                    activityIndicatorPublisher.send(false)
                })
            .tryMap { data, response -> Data in
                guard let httpResponse = response as? HTTPURLResponse,
                      httpResponse.statusCode == 200 else {
                          throw NetworkError.httpError
                      }
                return data
            }
            .decode(type: [Repository].self, decoder: JSONDecoder())
        .map { $0 }
            .catch { err in
                return Just([])
            }
            .eraseToAnyPublisher()
        return publisher
    }
Fabrizio Prosperi
  • 1,398
  • 4
  • 18
  • 32