
Member-only story
Design Pattern
Swift Patterns with Promises
How to use promises to write cleaner code
Hi everybody, I’m Riccardo. Senior iOS Engineer at Bending Spoons, I breathe iOS development, both apps and tools and I love to share my knowledge with others
This is the second article on how to use promises in Swift. If you want to know what a promise is and why it can be useful, have a look at the first part.
Once we know what is a promise and why it can be that useful, we would like to know when and how to use them. Today I want to show you three patterns that can simplify a lot the code we write in our apps.
We will start seeing how to use the promises to get the returned value from a dialog; we will see an evolution of that pattern, obtaining the user input from a modal view; finally, we will see how to use promises to download some resources from the web and use the download results immediately.
Promises with Dialogs
Without promises
In many occasions, we would like to ask the user to insert some input. The normal flow is usually:
- Prepare the UI.
- Present the UI.
- Wait for the user to interact with it.