How to Use PromiseKit in iOS Development with Swift

Yusuf Demirci
3 min readMay 8, 2020

--

Problem

Calling multiple functions in a sequence is complicated.

Solution

We have to build a chain in which its items communicate asynchronously. We can able to add new items to any position of the chain. PromiseKit is a tool which helps us to build the chain, in a structure.

Have you ever called more than one function at the launching of an application? Of course, you have called. Probably, you have checked camera permission and location permission at the same time. What about other checks such as if the user logged in, sending the request for the first data, etc. I am going to use PromiseKit to clean this mess.

What are we going to do?

We are going to build a simple one-screen project. As soon as the first screen launches, we will check if the user did log in, camera and location permission. After the permissions, we will send two requests parallelly. When the chain completed, we will give data to the view. If any error occurred before complete the chain, the chain will interrupt and we will show an alert. I have finished a working example here. Please download and open it before we start. Don’t forget to install pods before opening it.

We have one controller. Therefore we are going to analyze step by step.

HomeController.swift

Between 41. and 55. lines, you can see the chain. PromiseKit has some block keywords to know before we start.

firstly: The first block of the chain.

then: The block after the firstly. Can be used more than once.

done: The block which calls after the chain completed successfully.

catch: The block which handles error.

finally: The block which calls after the chain completed with success or failure.

In the firstly block, we call the checkIfUserDidLogin() function. The function returns Promise<Void>, which means a promise with Void type. I have declared (promise, seal) object in 65. line. Seal is a resolver that resolves the promise as success or failure. The seal returns success 68. line and returns fail in 70. line.

You can see the same logic in the checkCameraPermission() and checkLocationPermission() functions.

There is a specific case exclude of them, how can we call two or more functions in parallel? Assume that we want to make multiple service calls at the same time because these multiple services are not related to each other, so they don’t have to wait for each other.

PromiseKit has a function for that, when. In the 48. line I call the when function and give it an array which includes sendFirstRequest() and sendSecondRequest() functions. These two functions are called at the same time. When two of them are done, the chain completes, so done block at the 50. line works. It means the screen is ready to initialize view.

As the chain has been completed successfully, we can dismiss the loading view in the finally block in the 54. line.

What about errors?

The functions we called in the chain, may throw an error, so we have to make sure the chain is broken safely. PromiseKit provides the catch block, which returns Error class, to handle errors.

In the 70. line, I called reject() function to break the chain, which means the other functions in the line will not be called. After calling the reject function, the catch block calls. I have created an error class ErrorModel, which must implement Error class.

Conclusion

PromiseKit provides some well-structured blocks to manage a chain. It is super easy to add or remove a call over the chain.

Unlike PromiseKit, if you are a ‘native’ boy, you can build your similar chain by using DispatchGroup.

Full project → https://github.com/demirciy/FastlaneExampleiOS

Keep following for more!

Note: Please comment if any correction or better way exists then you can help everyone.

--

--

Yusuf Demirci
Yusuf Demirci

Written by Yusuf Demirci

Mobile Developer, Indie Maker, Freelancer - yusufdemirci.me

No responses yet