When I started the project that became Using Combine, it was right after WWDC; I watched streamed WWDC sessions online, captivated like so many others about SwiftUI. I picked up this idea that SwiftUI was “built using the new framework: Combine”. In my head, I thought that meant Combine managed all the data – notifications and content – for SwiftUI. And well, that ain’t so. While the original “built using Combine” is accurate, it misses a lot of detail, and the truth is a bit more complex.
After I finished my first run through drafting the content for Using Combine, I took some time to dig back into SwiftUI. I originally intended to write (and learn) more about that. In fact, SwiftUI is what started the whole segue into Combine. I hadn’t really tried to use SwiftUI seriously, or get into the details until just recently. I realized after all the work on examples for Combine and UIKit, I had completely short shifted the SwiftUI examples.
Mirroring a common web technology pattern, SwiftUI works as a declarative structure of what gets shown with the detail being completely derived from some source of truth – derived from state stored or managed somewhere. The introductory docs made is clear that
@State was how this declarative mechanism could represent a bit of local state within a View, and with the benefit of Daniel and Paul’s writing (SwiftUI Kickstart and SwiftUI by Example), it was also quickly clear that
@ObservedObject played a role there too.
The Combine link to SwiftUI, as it turns out, is really only about notifying the SwiftUI components that a model had changed, not at all what changed. The key is the protocol from Combine: ObservableObject (Apple’s docs). This protocol, along with the
@Published property wrapper, does the wonderful work of generating a combine publisher – the default type of which is represented by the class ObservableObjectPublisher. In the world of Combine, it has a defined output and failure type:
<Void, Never>. The heart of that
Void output type is that the data that is changing doesn’t matter – only that a change was happening.
So how does SwiftUI go and get the data it needs?
Binding is the SwiftUI generic structure that is used to do this linkage. The documentation at Apple asserts:
Use a binding to create a two-way connection between a view and its underlying model. For example, you can create a binding between a
Boolproperty of a
State. Interacting with the toggle control changes the value of the
Bool, and mutating the value of the
Boolcauses the toggle to update its presented state.
You can get a binding from ahttps://developer.apple.com/documentation/swiftui/binding
Stateby accessing its binding property. You can also use the
$prefix operator with any property of a
Stateto create a binding.
Looking around a bit more while creating some examples, and it becomes clear that some handy form elements (such as
TextField) expect a parameter of type binding when they are declared.
Binding itself works by leveraging swift’s property getters and setters. You can even manually create a
Binding if you’re so inclined, defining the closures for get and set to whatever you like. Property wrappers such as
@EnvironmentObject either create and expose a Binding, or create a wrapper that in turn passes back a
My take away is the flow with Combine and SwiftUI has a generally expected pattern: A model to be represented by a reference object, which sends updates when the data is about to change (by conforming to the
ObservableObject protocol). SwiftUI goes and gets the data that it needs based on what was declared in the
Binding to get to the underlying data (and potentially allowing the SwiftUI view to update it in turn if that’s relevant).
Given that SwiftUI views are also designed to be composed, I am leaning towards expecting a pattern that state will need to be defined for pretty much any variation of a view – and potentially externalized. The property wrappers for representing, and externalizing, state within SwiftUI are:
- @ObservedObject and @Published
@State is all about local representation, and the simplest mechanism, simply providing a link to a property and the Binding.
@ObservedObject (along with
@Published) adds a notification mechanism on change, as well as a way to get a typed
Binding to properties on the model. SwiftUI’s mechanism expects this always to be a reference type (aka a ‘class’), which ends up being pretty easy to define in code.
@EnvironmentObject takes that a step further and exposes a reference model not just to a single view, but allows it to be used by any number of views in their own hierarchy.
- Drive most of the visual design choices entirely by the current state
But that’s not the only mechanism that is available: SwiftUI is also set up to react to a Combine publisher – although not in a heavily predetermined fashion. An interesting aspect is that all of the SwiftUI views also support a Combine subscriber:
onReceive. So you can bring the publisher, and then write code within a
View (or View component) to react to what it sends.
The onReceive subscriber acts very similarly to Combine’s
sink subscriber – the single-closure version of
sink (implying a Combine pipeline failure type of
Never). You to define a closure within your SwiftUI view element that accepts that data and does whatever needs doing. This could be using the data, transforming and storing it into local
@State, or just reacting to the fact that data was sent and updating the view based on that.
From a “What is a best practice” point of view, it seems the more you represent what you want to display within a reference model, the easier it will be to use. While you can expose a publisher right into a SwiftUI view, it tightly couples the combine publisher to the view and all links all those relevant types. You could (likely just as easily) have the model object encapsulate that detail – in which case the declaration of how you handle event changes over time are separated from how you present the view. This is likely a better separation of concerns.
The project (SwiftUI-Notes) linked to Using Combine now has two examples with Combine and SwiftUI. The first is a simple form validation (the view ReactiveForm.swift and model ReactiveFormModel.swift). This uses both the pattern of encapsulating the state within the model, and exposing a publisher to the SwiftUI View to show what can be done. I’m not espousing that the publisher mechanism is a good way to solve that particular problem, but it illustrates what can be done nicely.
The second example is a view (HeadingView.swift) that uses a model and publisher I created to use the built-in CoreLocation framework. The model (LocationModelProxy.swift) exposes the authorization as a published property, as well as the location updates through a publisher. Within the built-in Cocoa framework, those are normally exposed through a delegate callback. A large number of the existing Cocoa frameworks are convertible into a publisher-based mechanism to work with Combine using this pattern. The interesting bit was linking this up to SwiftUI, which was fun – although this example only taps the barest possibility of what could done.
It will be interesting to see what Apple might provide in terms of adopting Combine as alternative interfaces to its existing frameworks. CoreLocation is such a natural choice with its streaming updates, but there are a lot of others that could be used as well. And of course I’m looking forward to seeing how they expand on SwiftUI – and if they bring in more Combine based mechanisms into it or not.