Last active
September 1, 2020 19:05
-
-
Save anupamchugh/20c8e8d204b520d3c4f61067792c3b29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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