Unlock This Episode
Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.
Introduction
Brandon: For the past few weeks we have gone deep into the techniques of sharing state in the Composable Architecture. When we say “shared state” we mean something very specific. We mean:
- A piece of state that can be easily accessed and mutated from multiple features such that when a feature mutates that state, every other feature instantly sees those changes.
- And often, such shared state is ubiquitous throughout the app and needs to be persisted. That is, many features need access to the state, and we do not want to have to explicitly pass it through multiple layers just to get it to a feature that needs it, and after relaunching the app the state should be restored to its last value.
And at the end of those weeks of deep study we came out the other side with a really wonderful solution. Sharing state in the Composable Architecture simply means holding that state in a reference type via the @Shared
property wrapper, and persistence is handled by a simple strategy that is passed to the property wrapper.
Stephen: That simple idea led to a very powerful and ergonomic way of sharing state, and it was only possible thanks to the wonderful observation tools that Swift 5.9 introduced. And also thanks to our back-port of those observation tools we can use the shared state tools immediately, no matter what version of iOS we are targeting. And we even put in a bit of extra work to make sure that shared state could still be exhaustively tested, even though typically reference types are very difficult to test.
So, all of that was really great, but we also didn’t get to show the tools being used in a real world application. We used a simple case study from the repo to get our hands dirty and figure out what the tools should look like, but let’s now see a much better example.
Brandon: We are going to take the SyncUps demo application from the repo, which is something we built from scratch in our tour series of episodes when the 1.0 of the library was released, and we are going to update it to use shared state where appropriate. Currently the SyncUps app does some annoying manual work to synchronize state between multiple features, and ideally shared state should make that much more natural to accomplish.
So, let’s see what problems we currently have with sharing state in the SyncUps app, and then let’s fix it.
Subscribe to Point-Free
Access this episode, plus all past and future episodes when you become a subscriber.
Already a subscriber? Log in
References
Getting started with Scrumdinger
AppleLearn the essentials of iOS app development by building a fully functional app using SwiftUI.
A Tour of the Composable Architecture
Brandon Williams & Stephen Celis • Monday May 4, 2020When we open sourced the Composable Architecture we released a 4-part series of episodes to show how to build a moderately complex application from scratch with it. We covered state management, complex effects, testing and more.
SyncUps App
Brandon Williams & Stephen CelisA rebuild of Apple’s “Scrumdinger” application that demosntrates how to build a complex, real world application that deals with many forms of navigation (e.g., sheets, drill-downs, alerts), many side effects (timers, speech recognizer, data persistence), and do so in a way that is testable and modular.