New to Promises and how to use them in iOS development? then please feel free read this short introduction I wrote previously on this topic: https://hackernoon.com/promise-pattern-on-swift-with-promisekit-620efad7a5bd
Promises are fun I swear!
On my previous article, I showed some examples on you how you can convert your async call pyramids into a more organised chain of promises like this:
More so, wrap them up without changing your original API implementations and end up with something like this:
The above implementation was possible by creating a single reusable wrapper function that provides you the closure you need when handling async callbacks:
The wrapper function holds reference to the completion block that you can give to your async calls and then return you a Promise object that you can listen to instead. Here’s a more detailed implementation of our custom wrapper:
With this in mind, let’s see how else can we tidy up in our code ~
Social media logins are pretty common in application designs and have even been quite a staple user requirement to most of our apps. Because of this, Facebook SDK’s LoginManager and GraphAPI have been two very familiar libraries that are often part of our architecture.
Third party SDKs are neat for several reasons:
But of course they may also come with some drawbacks, especially:
And Facebook SDK is not any different. In this very particular case, we will be looking at how we may integrate Facebook API calls into our Promises chain.
What makes Facebook an easy Social Integration to apps is LoginManager.
But it having its own a async callback may create some problems with your data flow especially when you have already designed them through Promises. Let’s take a look at its ‘completion’ callback and see how can we design a wrapper for it.
What we want here is to be able to replace the completion closure ‘((LoginResult) -> Void)? = nil’ with one that is from our wrapper function.
So first, let’s declare our new wrapper that takes a completion block whose parameter type is the same as the login completion:
Next, let’s prepare the Promise object by using the completion block parameter to trigger the Promise’s fulfil() and reject(). And while we’re at it, let’s go ahead and return the resulting Promise, using the LoginResult’s successful object type of course.
And when that’s all well wrapped up, you should be able to chain your Facebook login calls just like this:
Voila, no need to reimplement anything. Just wrap them up :D
Having built so many Restful Apps, my team and I are so used to organising and chaining asynchronous calls neatly with PromiseKit whenever we get the chance.
After what we’ve learned here, I’m encouraging you to look through your code base as well to hunt down those pesky async call pyramids and see a good solution for cleaning them up.
I wish you best of luck!
I hope that you got what you’re looking for when you opened this article. If so, I would very much appreciate it if you would recommend this to your friends ^^
If you have any more questions/feedbacks, please feel free to drop a message!