Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class DataFetcher : ObservableObject{
var cancellable : Set<AnyCancellable> = Set()
static let shared = DataFetcher()
func getJokes(completion: @escaping ([ChuckValue]?) -> Void){
let urlComponents = URLComponents(string: "http://api.icndb.com/jokes/random/10/")!
URLSession.shared.dataTaskPublisher(for: urlComponents.url!)
.map{$0.data}
.decode(type: ChuckJokes.self, decoder: JSONDecoder())
.eraseToAnyPublisher()
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { completion in
}) { response in
completion(response.value)
}
.store(in: &cancellable)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment