Thursday, July 7, 2022

Model View Controller Store: Reinventing MVC for SwiftUI With Boutique

Joe Fabisevich (tweet):

Apple has never provided a blessed architecture for SwiftUI, and many developers have spent thousands of hours filling the gaps with their own ideas. A familiar approach for many developers is to take the MVVM pattern many people adopted in their UIKit/AppKit apps and translate it to the needs of SwiftUI. That can work well enough, but you begin to see some cracks in the architecture when you need to manage state and data flow due how heavily SwiftUI leans on having a single source of truth. Others have taken the path of integrating powerful libraries such as The Composable Architecture which provide you with the tools to reason about your entire application. TCA takes inspiration from redux, more specifically The Elm Architecture, two patterns that are rather incredible in how they allow you to define your entire application as a tree of state. But TCA’s great power comes with great responsibility a very high learning curve, which can make it difficult to learn personally. TCA’s goals are much more of a fit for solving problems with a very high level of complexity, which may not be necessary for every app.

[…]

You can think the Store as the storage for your model objects, in SwiftUI this would be your single source of truth. If you model your data correctly then your user interface will always do what you expect it to do. That relationship between data and user interface is why Views having a single source of truth is so important.

I’ve built a batteries-included Store that comes with everything you’ll need out of the box called Boutique to be the foundation for that data. Boutique does no behind the scenes magic and doesn’t resort to shenanigans like runtime hacking to achieve a great developer experience.

Bodega:

Bodega is a straightforward actor-based library for writing files to disk with an incredibly simple API. Bodega is fully usable and useful on its own, but it’s also the foundation of Boutique.

[…]

Bodega provides two kinds of storage for you, DiskStorage and ObjectStorage. DiskStorage is for writing Data to disk, and ObjectStorage builds upon DiskStorage allowing you to write any Codable object to disk using a very similar API.

Both DiskStorage and ObjectStorage are implemented as actors which means they take care of properly synchronizing disk reads and writes.

Boutique:

Boutique is a simple but powerful persistence library, and more. With its dual-layered memory + disk caching architecture Boutique provides a way to build apps that update in real time with full offline storage in only a few lines of code using an incredibly simple API.

[…]

Storing images or other binary data in Boutique is technically supported but not recommended. The reason is that storing images in Boutique’s can balloon up the in-memory store, and your app’s memory as a result. For similar reasons as it’s not recommended to store images or binary blobs in a database, it’s not recommended to store images or binary blobs in Boutique.

[…]

The Store is a simple way to gain the benefits of offline storage and realtime updates, but by using the @Stored property wrapper we can cache any property in-memory and on disk with just one line of code.

Previously:

Comments RSS · Twitter

Leave a Comment