Swift Result – Hands on Example with Firebase

Hi, Alexi here from rebeloper.com and welcome back to the channel and welcome back to the second video of our series on result in Swift 5 So without further ado let’s jump right into it Now if you haven’t already done so go ahead and take a look at the first video. In this series where I basically set up everything. So in this video we can talk about result and all of its awesomeness. So now that we have this created let me just a build and run again. So we can see where we left off So basically we have an image view and an activity indicator spinning there and we want to download this dog.png that we have added to our firebase storage Now if we click on a dog.png and just open up this file location you will see that we have a download URL and that download URL is what we need to fetch So let’s do just that and let’s just create a new function down here and let’s make it a file private func and let’s see fetch firebase storage asset and let’s add in some parameters here because as you recall we have added a reference path of image name and an image type here. So we are going to use this reference path first. So fetch firebase storage from reference path it is a string named again a string and maybe let’s add a type dot string and for this type we will add in JPEG as a default value. Okay and basically that is it. Now because you want to have a completion Handler and yes that is what result is all about making completion handler is much much easier so let’s add our completion right now So completion and what we would usually do let’s make this completion here. There we go and what we would usually do is add in here let’s say an image so because we want to get back an image. So image it will be of type UI image and of course it’s optional because maybe we don’t get back an image or we can’t cast that image. So finally we would want to add in here an error and that could be of type error and again this would be optional and I believe we still need to add in here at escaping, okay. And that is how we would do that. So let’s go ahead and create this function in the old way and then we will refactor it with result Okay, so what do we do first. Well we need to create storage reference. So let reference equals storage dot storage reference and then we can append child with path and we need to construct this path So this path will be the reference path That we have added here in our function and then we need to add in here like so a slash and named and of course dot because now we need to add in the type and type. So that is our reference Now we need to get the download URL So reference dot get download let’s see download URL and under completion we will get an optional URL and an optional error. So now we need to take care of this. So if let error equals

error we need to take care of the error first. If we do have an error then we need to complete completion with the image nil and this error okay and we need to return. Now if we don’t have an error we need to guard let unwrap our URL. So guard let URL equals URL else and if we don’t get valid well if you don’t get a URL that is not nil then we need to complete with this completion nil and this time we don’t have any errors so we complete with nil and and of course we need to return So now we do have our URL and we need to create a URL session next. So let’s do that URL session leave its data task and let’s see here we need to have shared session and data tasks with a completion handler So there we go and before we do that this yeah I believe this is a valid URL so let’s do that let’s add in here our URL and for the completion handler we get back our data response and an error and of course all of these are are optional So again we need to go through the error handling so I’ve just simply copy and paste that in there and we don’t really care about the response at this time but yeah we could handle that but we guard let unwrap our data. So let’s paste that in there and in there. So now we do have our data and of course you know we complete with nil and error nil and nil as we did in the previous one So now that we do have our data we need to create a UI image. So let’s do that So let image equals UI image and let’s choose the one with data and as you can see this is an optional UI image so we will guard that unwrap it in just a moment. So let’s choose this and enter data, there. So now we can complete rather yet let’s just complete because we do have this image as an optional So we really don’t need to guard let unwrap it So let’s type in completion and image and the error this time is nil So let’s see if this went away it should go away and yeah that is it So how do we actually use this? Well let’s scroll all the way up to setup views well under setup views in the viewdidload let’s use this fetch firebase storage asset function. So fetch firebase asset from and the front is basically the reference path, reference path that we have created named is image name and the type and yeah I have chosen the wrong one so let me just do that again and we have two here so we will choose the one with the tie because we don’t have jpg we have png. So a reference path here image name for the name and the type is image type, okay So let’s take a look at our completion we get back an optional image and an optional error. So image here and an error. Of course we need to take care of the error again. So let’s see let me just copy and paste that and as you can see we are copying and pasting quite a lot and of course we are not completing here so we will print out something like failed to fetch image and we will add in here the errors localized the description, okay That takes care of that and now we need to guard let unwrap our image. So let’s do that here and just copy and paste

that so we can fix that and yep. We just print out here failed to image and the error should be no image available or something like that, okay And now that we do have our image now we can finally add this to our image views So image view there we go dot image equals image Now because we are on the background thread and in a completion first of all let’s add in self here because we are in a completion and because we are on a background thread in the URL session and good to know that I just remembered that we are on a background thread because we need to call resume on this URL data task you are a session data task. So we need to go back to our main threads so I’ve been here dispatch queue dot main and dot async now you choose the one with this code and I will just simply add in there and now we do have our image view on there. So let’s build and run and hopefully we do get our image back then we will punish the app further, okay. There we go It’s fetching and hurray it has been fetched, but if you look really, really closely you can see that the activity indicator is still spinning and we have this ugly background color. So let’s get rid of those two. So self dot image view dot background color equals dot clear, okay. And finally let’s stop our activity indicator, self activity indicator stop animating. So let’s build and run one more time and we should be good to go. It’s spinning There we go. Real nice, okay Now that is the way we would do it in let’s say in Swift 4.2 or earlier Now with Swift 5 we have a new way of doing this and that is with the result Okay, so let’s just create a new version of these because I don’t really want to type everything in here. So I will just copy and paste this and comment out the old version and yeah I will just simply copy, comment this out and we’ll do the result way of handling this fetch fire based storage asset. So there we go now here you can see that the under completion we have an image, a UIImage and an error. Now the way to do this with result is just simply go ahead and type in result you can see it’s auto completing and now you want to add in here a UIImage and error, okay And basically that is it. Now you will see some errors here because yeah we are not completing but it is much more simpler So what do we have to do? First of all let me just comment out let’s go from the top to, from the bottom to the top okay So we need to change the completion and simply just go in there or rather yet type it in all together so completion and just starting out let’s add in here a dot and now you will see that we have a failure and a success and that is the good part here because with the result you get a failure or a success So in this case we have a failure and the failure is at this error of course we need to provide an error here, okay Now that we have our image down here we can add dot and success and for the success we need to add whatever we added here So a UIImage and that is what we add in here, okay. That is pretty straightforward. Let’s go and do this

guard let data because now if we type in here we don’t really have an error Oh, let’s just fix this before we move on let’s just fix this we need to guard let unwrap this So guard let image because as you can see we cannot really add an optional there else and that we want to return but before we do that we want to guard let image, okay. We want to complete So completion and now we want to ad in here something like failure and again we don’t have that error and that is where this error plus that we have had it here and our error static let constants come into play because we have created our own custom errors and they are no URL, no data or no image and the no image fits real a nice here because if we cannot have an image unwrapped from our data then we should say that okay we don’t have an image. So we should say here custom error or I believe that is what it’s called or not. Yep, it is called differently firebase storage fetch error. Let me just copy that out. There we go. And if we add a dot here we have our completion and then we choose no image. The same goes for the data if we don’t have any data then we should complete with a failure and there we go firebase storage fetch error and no data. Now you know that why we created these error possibilities Here again we have dot failure and failure is a firebase storage fetch error and no URL, okay. And as usual as we did here we just complete with our failure error, okay. And I believe all of the errors have gone away let’s just hit command B here so we can see that build has succeeded there we go and how will we actually fetch this. So here comes the real real advantage and you will see in just a moment of course you can already see that it’s much easier so we are handling failures or successes here but the real nice thing comes right now when we basically use this function. So fetch fire base storage assets and I will believe I will use this one and that has the type it wasn’t that one, so asset and there we go and believe it should be that, that one, okay. So it’s kind of the same so we entered the reference path named is image name and the type is image type. Now under completion you can see that we have a result that has UIImage and an error in it So let’s tap on there and what I want to add in here is basically just type in here result and it’s real nice. So it’s a result it’s not something like image or error, okay. Now how do we get back how do we handle this result Well it’s basically with switch statements So switch and result unfortunately the switch statement auto-completion does not come up we’re inside our closure so we need to take care of this and switch must be exhaustive so I would just step on here and do you want to add missing cases yes I do want that so let’s fix that as you can see he will we have a case for a success and case for failure and for the success I will add in here lat image and for the failure we’re typing here let error. Because we want to use those. So for the error we are just simply going to print out the errors localized description. There we go, failed to fetch image with error and for the image we are going to use what’s inside

the dispatch queue main async So let’s uncomment that copy that and comment that back in and just use paste it in. Okay and now you can see that it’s much more simpler, okay So let’s build and run so we can see that it really does the same thing that we did with the old way of fetching Here we go. It is fetching, it is fetched the activity indicator, stopped animating and we have a clear background color for our image view and that is it. The advantage here is that now we can have a success case and a failure case. Now that is it for result. I hope you enjoy this video if you did go ahead and give it a thumbs up subscribe to the channel and hit that notification bell. Now it is your turn go ahead and implement result into your own completion handlers and do use this very convenient success and failure switch statement and as always I will see you in the next one