Implementing Generics JSON Fetching
AppStore JSON APIs
In this lesson, I'd like to go over the techniques of code reduction using Generics for JSON fetching. I'll first go over the syntax for a Generic Stack, and then move onto defining out generic fetch function, this way it'll be easier to understand. Make sure to leave questions down below for further clarification.

Comments (8)
Tube
4 years ago
Great class, pedagogically speaking. You know you could also sell courses on how to create courses Also, I wanted to suggest to you creating an Android or whatever class for Lynda. They make big bucks! But, leave Swift on here, so I can afford it. ;-)
richardbranson
4 years ago
Before watching this video: Generics is an advance concept and difficult for me. After watching this video: I've fallen in love with Generics. It's really not that difficult.
Brian Voong
4 years ago
rgarcia
4 years ago
Hi Brian, I'll love another Generic lesson specially to reduce code in the UICollectionViewController must be great. Thank for an amazing explanation with the stack example was great
duypham
4 years ago
Please proceed! We are really excited and can't wait for that. Thank you so much
ZiadHamdieh
4 years ago
YES PLEASE!!!!
Dennisvm82
4 years ago
Awesome :-D just what I wanted to learn. Generics are so much fun to work with.
ravikanth.marri@gmail.com
4 years ago
Excellent, your teaching skills are amazing. In your future courses if possible please use Result type enum with cases: success and failure.
shawnbierman
4 years ago
Brian, Is it not possible to further refactor some of these functions in the Service class? Is it possible to replace all of them with the generic call and just pass in the model somehow? Something like this: dispatchGroup.enter() Service.shared.fetchGenericJSONData(withModel: [SocialApp], urlString: "https://api.letsbuildthatapp.com/appstore/social") { (apps, error) in dispatchGroup.leave() self.socialApps = apps ?? [] } Then there would just be the need for the one generic function. Additionally, this really helped me understand generics better. I've been trying to solve a problem at work that this is perfectly suited for. Thank you.
Brian Voong
4 years ago
dev-tf
4 years ago
hi Brian, could you please explain what difference between @escaping (AppGroup?, Error?) -> ()) and @escaping (AppGroup?, Error?) -> Void)
Brian Voong
4 years ago
dev-tf
4 years ago
thanks Brian! Also i notice on the app store between each category and between each cell row inside the collectionView, there are gray separator lines, i used tableView before in storyboard and there is separator inset option, does collectionView have something similar?
Brian Voong
4 years ago
dev-tf
4 years ago
so it would be a manually UIView on each cellRow? And i know appStore doesn't have the slide to delete/slide to selection option, but would you do an episode on how to implement the sliding action ?
serxhio
4 years ago
Hey Brian, came back to this section to ask you for a quick fix. Since Swift 5 new Result type, I'm trying to implement it by doing this : func fetch<T: Codable>(url:String, completion: @escaping (Result<T? , NetworkError>) -> Void) However when I call it, it wont allow me to do this : Service.shared.fetch(url: jsonURL) { (request: AppGroup? , error) I would like to be able to pass the model type when I call the func, but is giving me errors, how should I modify that? Thank you!
mitul
4 years ago
Service.shared.fetch(url: Your-URL-Here) { (result: Result<YourTypeHere, Error>) in .... .... }
Yan Cervantes
3 years ago
Hi Brian this a question about good practices, I create for a SearchController a file where I put the ServiceNetwork class I Called SearchNetwork, and that service network have a parent class Service... and there I put the singleton, and the function to fetch results in the search text,... do you thing this is a good practice? for every class? create a file to get the correspond functions, because maybe in Service class, could be very big and the Service class have only the fetchGenericJsonData function... something like this.... and what do you thing about this? class Network { // Generic json function func fetchGenericJsonData<T: Decodable>(urlString: String, completion: @escaping (T?, Error?) -> Void) { guard let url = URL(string: urlString) else { return } URLSession.shared.dataTask(with: url) { (data, response, error) in if let error = error { completion(nil, error) } guard let data = data else { return } do { let appGroup = try JSONDecoder().decode(T.self, from: data) completion(appGroup, nil) } catch let error as NSError { completion(nil, error) } }.resume() } } class SearchNetwork: Network { static var shared = SearchNetwork() func fetchApps(searchTerm: String, completion: @escaping (SearchResult?, Error?) -> Void) { let urlString = "https://itunes.apple.com/search?term=\(searchTerm)&entity=software" fetchGenericJsonData(urlString: urlString, completion: completion) } } by the way I actually use Xcode 11 and everything goes fine
HELP & SUPPORT