Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created October 25, 2017 11:20
Show Gist options
  • Save azamsharp/c98b9663005090e7d94a5ddad0d0153c to your computer and use it in GitHub Desktop.
Save azamsharp/c98b9663005090e7d94a5ddad0d0153c to your computer and use it in GitHub Desktop.
extension ArticleListViewModel {
func get(completion :@escaping (ArticleListViewModel) -> ()) {
// Use a URL builder scheme here instead of just using the string URL
let url = URL(string :"https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=0cf790498275413a9247f8b94b3843fd")!
// Instead of getArticles method webservice should be generic and return the requested
// object/objects of requested type
self.webservice.getArticles(url: url) { articles in
self.articles = articles.flatMap { article in
ArticleViewModel(article: article)
}
DispatchQueue.main.async {
completion(self)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment