Let's write some C as an oriented programming language
[iOS] How to combine Delegates and Closures
Hi astronauts, today we will keep talking about mobile development, and as always native style.
This article is following the 2 previous ones, and it's mandatory that you read them before, in order to understand what's going on in this one:
If you have already read the 2 previous articles, I guess you already have an idea about what this one is about.
- Yes we already know, so please hurry up, we want to know how we can make something as sexy as the listeners but on iOS this time.
- Ok, just a bit of technical chit-chat before, and then were are ready to go.
How are we going to proceed?
As in the first article, so that everyone is happy, I will provide some DUMMY code for both Objective-C and Swift.
As you may know in the iOS universe, we can use both Delegates and Closures. Usually closure is used for more flexibility and it is also easier to implement. However, in some cases, graphic components for example are just made to work with a delegate or a datasource. Right now I'm thinking about 2 components: UITableView and UICollectionView.
With those 2 components, you cannot use block/closure, and you have to use a good old delegate. Usually it's not really a big issue and you can just work like that. But let's say that you have to work with many of those components on a single screen, in this case the code can start to be really messy. You'll find yourself with some huge classes, and it really starts to be difficult to produce some beautiful and elegant code. What I would like to show you today is a solution, that in my opinion is pretty clean and easy to set up.
A little setup
As in the two previous articles, we will proceed with a GET call on a URL and build a system that will notify us in case of both success and error. We will go a little bit faster than in the first article, because those are notions that you are already suppose to master.
It's time to talk about code!
Our goal is to realize a class that performs a GET call on a URL. I want to notify the object that launched this request if it failed or succeeded. To avoid strong dependencies, we will use the delegate pattern, thanks to this I don't need to know the exact type of the object.
Let's do this in 3 steps:
- Write the protocol
- Write the blocks/closures
- Write a class that will inherit from the protocol and that contains our 2 blocks/closures as attributes.
We will now implement the class that will inherit from the protocol. It will contain the 2 methods onRequestSuccess and onRequestFailure and each of them will call the dedicated block/closure.
Then, we code the class RequestManager that you are familiar with.
Then, we code the method to call our webservice.
Let's take a look on what we just coded.
- We instantiated our Manager, that will call the webservice
- We defined our two blocks/closures
- We instantiated our Delegate
- We assigned our two blocks/closures
- We assigned the Delegate to the Manager
- We called the webservice
Here is the Swift code for the most excited ones
Now, if I call the method callWebService, considering the dummy code we produced, the result will be a call to the block/closure requestSuccess.
But why do we bother to do all this?
Indeed, why do all this, especially in our case? We could just have used a Delegate or Blocks/Closures as we did in the first article. This adds a lot of complexity in the code, and it looks like we are doing the same things twice... As I told you at the beginning of the article, this solution comes for a specific case. To make a Delegate more flexible when you have no other choice that to use this pattern.
- If the Protocol contains a lot of methods, then we need to re-implement a lot.
- We also must define all blocks/closures related.
- We need to redefine the blocks/closures for every call.
- More flexible delegates
- Clean code
- Reduced methods
- Thinner handling for the callbacks of the Delegate
This solution is not perfect, but still quite elegant and is not too heavy to implement. Then, I'll let you test it and give me some feedback in the comments section.
See you space cowboys :)