🎉 End-of-year Sale! Save 25% when you subscribe today.

Observable Architecture: Observing Collections

Episode #263 • Jan 8, 2024 • Subscriber-Only

We can now observe struct, optional, and enum state in the Composable Architecture, but what about collections? Let’s explore what it takes to get rid of the ForEachStore wrapper view for a vanilla ForEach view instead, while still observing updates to collection state in the most minimal way possible.

Collection
Observable Architecture
Observable Architecture: Observing Collections
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

Introduction

Brandon

Thanks to the new Observation tools in Swift 5.9 we have now gotten rid of 4 specialized view helpers that previously existed only to aid in minimizing view re-computation. They are WithViewStore, IfLetStore, SwitchStore and CaseLet. We now get to use simpler, more vanilla Swift code to constructs these views, and they still minimally observe only the state that is touched in the view.

Stephen

But there’s another really powerful and popular view helper that ships with the library that allows you to decompose complex list-based features into smaller units, and that’s the ForEachStore. With it you can easily transform a store of some collection of features into individual stores for each element in the collection. This allows you to have a dedicated, isolated Composable Architecture feature for each row in a list.

It would be amazing if we could get rid of this concept too, and just use a vanilla SwiftUI ForEach view. Well, this is absolutely possible, and it greatly simplifies the way one deals with lists in the Composable Architecture.

Let’s take a look.

Getting rid of ForEachStore


Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing