Login required for access
In order to watch this lesson, you'll need to login to the website.
How to Fetch Multiple Feeds
YouTube
Today we'll look at how to fetch multiple feeds for the Home, Trending, and Subscription sections of our app. We'll be doing this intelligently by subclassing our current FeedCell. Lastly, I'll show you many techniques to reduce the lines of code in your project. Doing so will help prevent bugs in the future. Enjoy.
!codebreak <div class='filename'>HomeController.swift</div> !codebreak !syntax-highlight class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { let cellId = "cellId" let trendingCellId = "trendingCellId" let subscriptionCellId = "subscriptionCellId" //... func setupCollectionView() { //... collectionView?.registerClass(FeedCell.self, forCellWithReuseIdentifier: cellId) collectionView?.registerClass(TrendingCell.self, forCellWithReuseIdentifier: trendingCellId) collectionView?.registerClass(SubscriptionCell.self, forCellWithReuseIdentifier: subscriptionCellId) //... } //... override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let identifier: String if indexPath.item == 1 { identifier = trendingCellId } else if indexPath.item == 2 { identifier = subscriptionCellId } else { identifier = cellId } let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) return cell } //... } !codebreak <div class='filename'>Trending.swift</div> !codebreak !syntax-highlight class TrendingCell: FeedCell { override func fetchVideos() { ApiService.sharedInstance.fetchTrendingFeed { (videos) in self.videos = videos self.collectionView.reloadData() } } } !codebreak <div class='filename'>SubscriptionCell.swift</div> !codebreak !syntax-highlight class SubscriptionCell: FeedCell { override func fetchVideos() { ApiService.sharedInstance.fetchSubscriptionFeed { (videos) in self.videos = videos self.collectionView.reloadData() } } } !codebreak <div class='filename'>ApiService.swift</div> !codebreak !syntax-highlight class ApiService: NSObject { static let sharedInstance = ApiService() let baseUrl = "https://s3-us-west-2.amazonaws.com/youtubeassets" func fetchVideos(completion: ([Video]) -> ()) { fetchFeedForUrlString("\(baseUrl)/home.json", completion: completion) } func fetchTrendingFeed(completion: ([Video]) -> ()) { fetchFeedForUrlString("\(baseUrl)/trending.json", completion: completion) } func fetchSubscriptionFeed(completion: ([Video]) -> ()) { fetchFeedForUrlString("\(baseUrl)/subscriptions.json", completion: completion) } func fetchFeedForUrlString(urlString: String, completion: ([Video]) -> ()) { let url = NSURL(string: urlString) NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in if error != nil { print(error) return } do { let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) var videos = [Video]() for dictionary in json as! [[String: AnyObject]] { let video = Video() video.title = dictionary["title"] as? String video.thumbnailImageName = dictionary["thumbnail_image_name"] as? String let channelDictionary = dictionary["channel"] as! [String: AnyObject] let channel = Channel() channel.name = channelDictionary["name"] as? String channel.profileImageName = channelDictionary["profile_image_name"] as? String video.channel = channel videos.append(video) } dispatch_async(dispatch_get_main_queue(), { completion(videos) }) } catch let jsonError { print(jsonError) } }.resume() } }

Comments (4)
6 years ago
7 years ago
8 years ago
8 years ago
HELP & SUPPORT