nalexn / clean-architecture-swiftui

SwiftUI sample app using Clean Architecture. Examples of working with CoreData persistence, networking, dependency injection, unit testing, and more.
MIT License
5.73k stars 697 forks source link

Question regarding Value: Decodable and Closures #84

Closed fredagsfys closed 1 year ago

fredagsfys commented 1 year ago

Hey there, I'm new to iOS development in general but especially Combine/SwiftUI. I've followed your clean architecture and tried to extend it a bit by introducing credentials manager and injecting a token in the WebRepository call.

However, what puzzles me is that if I wrap following code below into a closure I get Generic parameter 'Value' could not be inferred.

session
  .dataTaskPublisher(for: request)
  .requestJSON(httpCodes: httpCodes)

Could you please advise on how to get this solved? This is the whole picture of the function.

func call<Value>(endpoint: APICall, httpCodes: HTTPCodes = .success) -> AnyPublisher<Value, Error>
    where Value: Decodable
{
    do {
        var request = try endpoint.urlRequest(baseURL: baseURL)

        credentialsManager.credentials()
            .map { (credentials: Credentials) in
                let token = credentials.accessToken
                request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
            }
            .map { (_ publisher: AnyPublisher<Value, Error>) in
                session
                    .dataTaskPublisher(for: request) // Generic parameter 'Value' could not be inferred
                    .requestJSON(httpCodes: httpCodes)
            }

    } catch {
        return Fail<Value, Error>(error: error).eraseToAnyPublisher()
    }
}