Advanced Swift Generics: Best Solution to Eliminate Code Duplication!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hey what is going on everyone welcome back to the channel hope you are having a fantastic day now if you're kind of like me and you've been running a lot of darn Swift code for the last three or five years you are probably able to notice a lot of repeated patterns inside of your application code base right and one of the most common areas that you'll find repeated code and a lot of copying and pasting is inside the networking module of your application so today's video I want to show you how to finally resolve this problem by introducing you to the idea of generics now one thing I want to say about generics before I get started with some code is that the idea of generics is actually kind of advanced it's one of the harder topics to learn instead of programming so just make sure you kind of stick with me for the whole video and you should be able to understand it fairly well so without further ado let's get started here and what exactly do I have on my screen besides Xcode on the left over here is the application that I have set up that pretty much does two things it has two screens one is the original course page right here and upon clicking into one of these rows we load some data regarding the course so you see four items over here and then finally you have one item for the last page all right so that is our very very simple application that I wanted to go over and basically we have two endpoints that kind of fetches the data that you're seeing inside of these two pages so the first page is the courses and you'll find that at this YouTube home feed endpoint and basically videos array right here is these three items so you see Instagram firebase you see intermediate training core data and then finally Kindle basic training and upon clicking into let's say this top item we are going to hit this endpoint which is YouTube course detail with the ID of one and you will see creating a registration the screen and the duration over there and all of these items inside of this array is reflected inside of this page over there all right so that looks pretty good and hopefully you understand the problem we are trying to tackle basically a lot of the applications that you write are going to have a lot of networking calls every time you create a new network called something that you might do if you're not writing your code correctly is you'll just copy and paste all the previous networking code and just modify a couple of different configuration parameters and that's something that you would like to avoid but it's kind of tricky on how you actually avoid that problem so let me show you how to do that right now by moving the browser out of the way and what I have set up on this Xcode project is something that's very very simple let me just run it first to show you what it does I believe we are just going to get a white screen so that's the white screen over here and we have some information being printed out inside of the console so what exactly is this well that is pretty much this for a screen over here and we are calling the fetch home feed which performs the fetch using the URL string of YouTube home feed which is this first URL over there Mewtwo bone feed and then we perform the data processing on line 29 using JSON decoder which because this home feed object over here and finally we call the completion handler that takes in a home feed and that's kind of how we print out everything using this line on line 17 so you see print dollar sign 0 and name and that's kind of how all this stuff works hopefully you are familiar with JSON decoder and how to use URL session and that's kind of the set up work we have so far all right so let's kind of get into the meat and potatoes of today's lesson and what I really want to show you is the problem that we are going to have whatever we try to make a second Network call which is this guy over here so let's just copy this inside of our clipboard and move back into Xcode so you see this screen right here right we click into this guy and we load a second view controller that fetches the second endpoint data and typically when you want to create a second now we're call you're just going to maybe copy and paste all that so why don't I do that and show you how this works so you copy the fetch home feed Network call you see it has this URL string you paste it or right below or somewhere inside of your application and what you do is you modify this guy and you say you know maybe a fast fetch details or whatever you want to call it and then you modify the URL string to be the second one over here at course detail ID equals one you copy that guy and then you change this URL over there and then the last thing that you want to change is probably what you decode using the JSON decoder and this guy is getting the home feed so you obviously don't want to do that instead you want to use something that represents this JSON data object so what I have set up already inside of this project is the other object over here called course detail is going to have the name and duration properties so that I can sort of read into these two values over there and so the last thing you want to do is to paste that course detail in there and you want to change this to an array because the object inside of this screen right here is an array and so you see it starts as an array like that and there you go so the last thing you want to do is you want to modify this over here so that it is actually a course detail array so let me just copy and paste that in there because we are going to call the completion block using this object all right so home view right here is a course detail array so why don't we modify the name of this to be a little bit different so that it makes a little bit more sense so of course detail goes over there and completion with course details all right so paste that and we are kind of ready to make a second called so instead of you download I'm going to call fetch details which we just wrote and we have this completion block we can say something like course details for our completion and instead of here you can just say course details and execute a for each block on this object and because it's an array you can just say something like print and dollar sign 0 dot name and you know name dollar sign 0 dot duration as well so let's just run this bit of code and hopefully you'll see this being printed out in the concept below so what you get is the original print statement of the home feet up there and then we have the course detail name and duration showing up on the bottom over there second so those are the two endpoints that we're trying to fetch data for and you can imagine that if you have a third or fourth endpoint you don't really want to copy and paste this stuff yet again because a lot of this stuff is just doing pretty much the same thing and it's a very generic in nature right and so here we go this is where the idea of generics comes into play here and the way that we are going to introduce generics is to make a third call and down here I want to say let's say file private and it's a third function I'll just call it fetch generic same generic data like that and the thing that we want to do is to introduce this generic symbol called a T and again this is kind of hard to understand at first until you have all the code written out so I'm going to write down the rest of this using the same syntax that we kind of have up over there so the completion block that we want to complete with is the actual T object over here and this is the completion block syntax like so alright so this T object is the generic that we have inside of our fetch generic function call and this T is kind of related to this T over there and now let me show you how to actually use this stuff all right so I'm going to copy this code one more time and just pop back in there all right so the thing about this call is that it's going to complain about this completion block over here because this course detail which is this guy is trying to use this completion block of type T and this guy is not a type T and it's all very confusing so what you really need to do to fix this problem is the object that you wants to decode out I'm just going to use T itself over there and this guy over here I'm just going to call obj and let's just say completion block with obj and everything should be fine all right the last thing that you need to do is you want to hit this fix button over here which will introduce the add escaping over there and there's going to be a one last err that tells you that this T object does not conform to the decodable protocol so the final fix is to make sure that your generic is actually a decodable like so and if you build one last time you'll see that this decode call which takes an aid decodable protocol is actually working now all right so what can we do with this new method call a fetch generic data is we can actually replace it this guy and just pop it in is that a viewdidload here so I'm going to replace fetch details right now with that same call so if fetch details are fetched generic data hit the enter over there and for the decodable object I'm going to use course details and I have to actually declare what this type is so that the generic can actually automatically infer what your generic type is so the generic in this function call is fast as course detail and instead of here you can do the exact same thing so you know you can say course details it shows you that this object is actually a course a detail object and it actually is an array so you want to modify it to be an array like that and again if you just type it out you see that it is a tower a type like so and finally you can say for each and you know you can do the exact same thing that you had here and everything shouldn't work out fine all right so you can build your project and you can run your code and hopefully the print statement that we had before will still show up like what we had earlier and that's exactly what we get the course detail object looks like is being printed out correctly and the last thing that we want to do with our fetch generic and data called is that the URL string that we're using is only going to reach out to the course detail endpoint which is the second guide and we want to account for having different URL endpoints right so the last thing we want to modify here is why don't we just introduce the URL string as a parameter inside of our function call so that's a URL string and let that be F type string and we can now comment that line out this URL string property is going to take it from the parameter of this function call alright so the last thing you need to do is you want to modify that over here as well so this is actually missing the URL string so I've hit that fix and I'll just pop that in there and let me just grab that over here so URL string fetch details so previously it was kind of using this thing over here so let me just go back up here for the URL string you can just use quote and I think I have the quote already pop that in there and here we go fetch generic data is now going to take in the URL string automatically inside of as method parameters and there we go everything looks fine alright so now let me finish off by showing you how to actually refactor the fetch home feed into using this fetch a generic data call as well so what I want to do here is to say if fetch generic is data the URL string is this over here for the fetch own feed so why don't I just copy that and for the completion block hit enter and again you want to make sure that you declare what this type is by saying home via type like that and the generic call will automatically be able to infer what your generic type is which is that guy over there so over here you can now say home feed it appears with the home P type and you can say dodge let's see videos I'm looking up here yeah for each and this guy is this statement not sure if I'm going to be able to type that out correctly so why don't I just copy and paste that in there alright so running one last time you can see that your generic called we will actually print out the exact same thing that we had before all right so with the introduction of the fetch generic data call you can pretty much eliminate all this unnecessary code right away so what we end up with is just one single method called fetch generic data it takes in a URL string parameter and it has this generic type that automatically tells your JSON decoder what object to decode out of it and then finally your completion block will call with the proper object being decoded out all right so this is the way that generics work the syntax I have to admit is a little bit confusing if you're just reading it for the first couple of times so the last thing I want to mention here is that let's say we have a third endpoint that we want to consume data from right so at this endpoint JSON decodable slash courses let me just copy that into my clipboard and let's make a call to consume that data very very simply by making a third call a fetch generic data using the endpoint of JSON decodable and courses and for the decodable let me just enter and here we go we have to now determine what the generic parameter of type T has to be because it currently cannot infer what the generic is so let me create that generic object right above that represents one of these objects over here so we have ID name and link and so this is a course object so let me try to show you how that works so let me just create a struct over here and call it course and make sure it conforms to decodable let's I think I had this as an int and let's say name be a type string and then the link have also typed strings so those three properties are just these three things over there and the last thing I need to do now is I need to just say courses and give this guy a type of a ray course like that and the reason why I'm doing it inside of an array is because this is that a read if it's not an array you can just specify the object itself and inside of here you can say courses dot for each and you can just print it out using print dollar signs zero and you can say I don't know link let's see I think that's what it needs to be let me try to run that and you should be able to see a bunch of links down in the description below so these are the links right there and because these calls happen asynchronously sometimes the actual print statements occur in a different order and that's kind of why you see this up here first alright so that's the power of generics and using this fetching generic data called you can kind of see how you can consolidate all of your networking code in just one simple function and now that you have just one function you can modify this guy to kind of handle a lot of other use cases for example we haven't really touched upon how to handle the error right so you know you can just type in here let's say if that error equals that error object from URL session you can print out you know failed to fetch data have print out the error over there in this return and so you can do whatever you want in here and as long as you modified this one single function you know that your changes will pretty much affect all of your network and call so that you can see the error occur instead of your print statement or instead of your UI alright everyone that's going to wrap it up for today's video on how to use generics to eliminate a lot of boilerplate code instead of your networking modules and again the idea of generics is definitely a more advanced topic instead of computer science and programming itself so don't be too concerned if you don't understand everything right off the bat just make sure to pay a little bit more attention to when you see this generic type of code all right that's going to be it for today if you enjoyed today's video make sure to hit the thumbs up button and also subscribe to the channel for more videos like this if you enjoy what I do and want to support me make sure to check out the courses down India link descriptions below and there's also going to be a link to the project that you saw in today's video as well and that's got to be it for me today keep on coding guys and I'll see you in the very next video bye bye
Info
Channel: Lets Build That App
Views: 33,549
Rating: undefined out of 5
Keywords: ios, swift, development, tutorial, learn, xcode, programming, code
Id: lpTNaBUFkno
Channel Id: undefined
Length: 17min 17sec (1037 seconds)
Published: Thu Jun 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.