Swift Composable Architecture

Tripti Kumar (@techieyogini)
TribalScale
Published in
3 min readOct 21, 2022

Written by: Tripti Kumar, Engineer Manager, TribalScale

📫 Subscribe to receive our content here.

💬 Have any questions about our development capabilities? Click here to chat with one of our experts!

Hey folks! Ever since Apple has released SwiftUI, more and more app developers are making the switch. We are getting more enhanced features in SwiftUI with every passing year. There are also some challenges still being faced to code effectively in SwiftUI. I recently came across a solution to the majority of these challenges: The Composable Architecture (TCA) Ta da…🎉

Why TCA?

To be honest, the majority of us are quite happy with MVVM as it’s working quite well so far. Some of us have even received the “Employee of the month” award too(Thanks to MVVM 🤫😎), so there’s no reason to switch to a new framework. We love MVVM 😍❤️.

We love MVVM!!
Photo by Anthony DELANOIX on Unsplash

Enter TCA with its best feature: “Side Effects”.

Side Effects is an implementation to let certain parts of the application talk to the outside world in the most testable and understandable way possible.

(Credits to https://github.com/pointfreeco/swift-composable-architecture developed by Brandon Williams and Stephen Celis.)

A few common use cases for Side Effects are:

  • Persisting data
  • Error Handling
  • Debugging
  • Error Tracking

What Else?

Other than side effects, TCA offers an easy and graceful solution to the following modules:

  • State Management — TCA helps to manage and share the state of the app throughout all screens. Thus, helping you with easy observation of any mutation happening within the app.
  • Composition — This enables “Modular Code”, code which is separated into small independent modules. These modules can be used to form different features.
  • Testing — This includes feature testing, as well as integration testing. This leads to a bug-free business logic.
  • Ergonomics — Accomplish all of the above in the simplest way.

Building Components of TCA

You can use these types and values to model your app:

  • State — A type that updates the dependent parts of the view hierarchy whenever it’s own value changes.
  • Action — It includes types like user actions, notifications, API request returns, etc.
  • Reducer — A pure function that takes the current state and the action to execute as parameters and produces a new state. A reducer will receive everything that it needs as parameters. It has no ties to any outside entities. Hence, producing a new State value.
  • Store — A runtime place that you send all user actions to the store so that the store can run the reducer and effects. Thus, enabling you to observe state changes in the store so that you can update UI.

Hopefully this information motivates you to try TCA on your own. You can find more information with coding examples and live use-cases on the TCA official website.

Thank you for reading! You can comment below for any queries.

Tripti is a curious techie and a philomath, working at TribalScale as an Agile Software Engineer. A big fan of “How I can” rather than an “If I can” mindset. She spends her time caring for plants and pets, doing yoga and meditation, and volunteering for Isha Foundation.

TribalScale is a global innovation firm that helps enterprises adapt and thrive in the digital era. We transform teams and processes, build best-in-class digital products, and create disruptive startups. Learn more about us on our website. Connect with us on Twitter, LinkedIn & Facebook!

--

--

Tripti Kumar (@techieyogini)
TribalScale

Tripti is an Engineering manager. She worked as an iOS app developer since 2011. Lately she’s been learning a bit of React, React Native, AWS, Hasura and GCP.