Working with the didSet Property Observer in Swift
Writing code that is autonomous and efficient in app development is critical for an app’s lifecycle and for user experience. Applications listen for user events and interactions to progress throughout the program’s flow. Under the hood, iOS facilitates numerous operations on its own based on multiple situations using functions and other means.
The use of Property Observers simplifies this clockwork automation by listening to certain events for a given property based on a condition to execute a block of code more often than not containing a specific function. We will be looking at the didSet property observer in order to allow our app to be more manageable and flow with ease.
The didSet property observer is used in junction with a computed property to listen for when the variable it is assigned to has its value set. Any changes to the variable in question will have its didSet block called automatically.
Imagine we are working on an app that has a start and stop button to measure time. Our app requires us to create a custom button; it is also important that our class uses the proper file access level keywords, making its methods inaccessible to other parts of our app.
To control the state of our button, we will use a computed property with a didSet property observer to listen and respond to the state of the variable to update our button.
By default our button will be in the starting position and our variable will be set to true. Within our didSet block we will add a switch statement that checks our variable and runs our button’s methods depending on the logic we set up.
Any time our isInStartingPosition variable changes our observer will be called, and the appropriate method will be called to change the state of our button. This is a great way to restrict the use of functions outside of a class, making a variable the only entry point to call methods appropriately based on the logic that is set up.
The didSet property observer can simplify code that relies on tasks to be complete such as making an API call or awaiting for a completion handler to be called. Imagine in our app that we are making an API call to download user data to be displayed to our user. Once our data is downloaded and populated into our data model our user will be passed to our viewController.
Our networking layer will present us with a user for our viewController to continue our apps flow; while this definitely works, it will be impossible to determine when our fetchUser function completes its task. With didSet we can automatically update our viewController’s views once our user variable has a value.
Once our downloaded user data is passed successfully to our variable, didSet will call our configureUI function to update our view’s state. It is important to note that didSet and other property observers require a computed property to run their code. The didSet property observer allows an app’s flow and logic more manageable, especially when awaiting for a task to be complete.