Hello Combine! Part 1 — How Combine change the way we use AlertController
Since Apple introduced Combine which helps us handle asynchronous events, so the developing logic seems to be better. Let me show you the difference when we use combine to handle AlertController instead of Closure.
To make it clear, we will develop an application called PickAWord. This Application will do basic stuff by picking up a word and then displaying it, easy right?
Start your engine and GO!
We’ll start by creating a new application project. Name it PickAWord or you can clone my initial project here.
You can try running the project. It will show you a basic screen with the message “Please, pick some word to say!”. So, we will start to modify this project.
Create ContentViewModel.swift to handle our logic there.
- When create a file, write down the class ContentViewModel like:
a parameter pickedWord will display on the screen which we will default as “Combine!” at the beginning.
a function pickAWord will execute when we tap the button on the screen. So, we will back to this later.
Modify our UI in ContentView.swift
- add a new text which will get a value from the ViewModel
- add a button that will call our pickAWord function.
Before we go back to ContentViewModel. We will create AlertUseCase which help us handle a picking word flow.
- Create a AlertUseCase.swift
- Add getTopViewController which finds a presenting view to show our alert
- Add function execute() with title, message, and buttons
Head back to a function pickAWord in ContentViewModel.swift and use an AlertUseCase and pick some word to display.
Look good right? Now, let’s start to use combine to handle this alert.
Head back to AlertUseCase.swift
- Add 2 helper classes, ENUM AlertResponseType and Struct AlertButton
- Add new execute which receives [AlertButton] instead of AlertAction
- My approach is using Future to wait for user’s response and FlatMap it to Publisher
Go to ContentView and change our button action to :
Then, go to ContentViewModel and add logic to switch the AlertUseCase to use a Combine Function.
Now, you can run and see what the difference.
project checkpoint here
What’s the difference
As you can see, The appearance does not have any difference! however, it’s a bit different in our code.
With Closure, you write down what you want to do when the user interacts with a button.
With Combine, you wait for the response and decided what will happen next.
let me show you more when the app is more complicated. we will add one more option to pick duo word.
Go to ContentView.swift
- Add new button “pick duo word” that call pickDuoWord(); next to “pick a word” button
Go to ContentViewModel.swift
- Add new function pickDuoWord which will pop up a second alert to pick a second word.
Let’s see how it work and do not forget to switch between Combine and Closure.
As a code above, you can see that the Combine will make your development logic more straightforward like “please ask a user for the answer and wait for it”.
Project checkpoint here
Summary
In summary, when your application goes bigger, more complicated, and has a lot of async tasks to handle. I think Combine should help you by making your code cleaner. Finally, what do you think about Combine? Share with me.
After all the way you read this article, feel free to give me some feedback and follow me if you love my article. ❤