Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Last active September 1, 2020 19:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anupamchugh/20c8e8d204b520d3c4f61067792c3b29 to your computer and use it in GitHub Desktop.
Save anupamchugh/20c8e8d204b520d3c4f61067792c3b29 to your computer and use it in GitHub Desktop.
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