Apologies
You must be signed in to watch this lesson.
Fixing Massive View Controller with Code Refactor
Podcasts
A very common problem that many newcomers to iOS development experience is Massive View Controller. This issue stems from developers writing too much code inside of our view controller files, typically code that can be separated into its own classes. For example, in our previous lesson, we built out the Alamofire networking code directly inside of PodcastsSearchController which wasn't exactly the best place to put it. In today's lesson, let's go ahead and perform some code refactor to move all network activity to an isolated class called APIService. This class's sole responsibility will be to perform network fetches for podcasts, episodes, and later downloading entire files for offline playback. Enjoy.

Comments (9)
Alpensol
6 years ago
Hey Brian, why are you using a Singleton Technique for a Networking Layer. Surely using a "Godlike" class is not a sustainable solution as more API call need to be made ? For example Authentication calls and data .get requests would be better off separate, no ?
Brian Voong
6 years ago
Alpensol
6 years ago
Thanks for the response, I greatly appreciate it. May I ask how you would recommend implementing a more solid networking layer for an app in that case, that is modular in nature and has good testability ?
Brian Voong
6 years ago
mghlm
5 years ago
Hi Brian, thanks for the course. I just wanted to say that I too would love a lesson on refactoring the code to get rid of the singleton and use dependency injection instead!
Tokyojogo
6 years ago
Hi Brian, great lesson on keeping the VC small. Question on the async search, don't we need to handle the results and put it in the main thread while its still searching?
Brian Voong
6 years ago
Tokyojogo
6 years ago
Oh 1 more thing, after doing this vid, I noticed that when running the simulator, when I click on the searchtab, i'm getting [MC] Lazy loading NSBundle MobileCoreServices.framework 2018-02-24 16:28:22.255180+0900 MyPodcastClone[5965:197565] [MC] Loaded MobileCoreServices.framework and when i click on the search textfield, I'm getting 2018-02-24 16:28:32.134838+0900 MyPodcastClone[5965:197565] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/jonathango/Library/Developer/CoreSimulator/Devices/FE27D17E-C382-49EE-BE52-44E0A0A1AEB9/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2018-02-24 16:28:32.137136+0900 MyPodcastClone[5965:197565] [MC] Reading from private effective user settings. 2018-02-24 16:28:32.228017+0900 MyPodcastClone[5965:197565] +[CATransaction synchronize] called within transaction 2018-02-24 16:28:32.228485+0900 MyPodcastClone[5965:197565] +[CATransaction synchronize] called within transaction 2018-02-24 16:28:32.229263+0900 MyPodcastClone[5965:197565] +[CATransaction synchronize] called within transaction 2018-02-24 16:28:32.229706+0900 MyPodcastClone[5965:197565] +[CATransaction synchronize] called within transaction Am I doing something wrong? How should I address this?
Brian Voong
6 years ago
byrne
6 years ago
quick question - in XCode, I'm not getting the 'Fix' on errors? Is there some setting I need to be aware of?
Brian Voong
5 years ago
edward
5 years ago
Hi Brian, really enjoying the podcast videos so for. In the completion handler, do you not need call this on the main thread? i.e DispatchQueue.main.async { self.podcasts = podcasts self.tableView.reloadData() } Also, in order to prevent a retain cycle, doesn't an [unowned self] need to be added?
Brian Voong
5 years ago
edward
5 years ago
ok cool. Only issue with using [weak self] is it requires you to make self an optional when it isn't, hence why I thought using [unowned self] more appropriate.
abhinavjayanthy
5 years ago
Hi,Instead of using a completion handler technically we ca use a NSNotification right? which one is better to use between both.
Brian Voong
5 years ago
abhinavjayanthy
5 years ago
So, I'd assume expect for number of lines and function , there are no memory related advantages or disadvantages
abhinavjayanthy
5 years ago
Thank you for the blazing fast response :)
Brian Voong
5 years ago
forsuresh
5 years ago
Inside func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) APIService.shared.fetchPodcasts(searchText: searchText) { (podcasts) in self.podcasts = podcasts self.tableView.reloadData() } option 2: APIService.shared.fetchPodcasts(searchText: searchText) -> [podcasts] == I like your usage of the @escape clause but if we went with the “option 2:” I listed above, I feel that I we could have returned an array of podcasts and then reloaded the tableView from calling function ‘func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)‘. Is the advantage of doing it with @escaping clause to help with code organization only? Or is there a performance/logic gain that I’m overlooking. Also, thank you for uploading such an excellent, informative video.
Govind K Kara
5 years ago
Hi Brian for networking if we use static class method instead of singleton is there any difference.Which is the best practice
Brian Voong
5 years ago
Christophe Bugnon
5 years ago
Hey Brian, love this course ! When will you made a full project with MVP or MVVM ? I'm intern and my company use MVP and sometimes it's hard to make difference between all these patterns. It use encapsulated protocol and function and often I'm lost with all these new things. What do you think about it ? Thanks a lot. Regards
HELP & SUPPORT