Thursday, July 6, 2023

SwiftUI Data Flow 2023

Discover Observation in SwiftUI:

Simplify your SwiftUI data models with Observation. We’ll share how the Observable macro can help you simplify models and improve your app’s performance. Get to know Observation, learn the fundamentals of the macro, and find out how to migrate from ObservableObject to Observable.

Sarah Reichelt:

With the introduction of Swift macros, the SwiftUI team was able to reduce the number of property wrappers need to send data around, and remove a lot of boilerplate code.

For this article, I have re-written my sample app as a Mac app and updated it to use the new data macros.

[…]

Apart from adding some details to the decisions points, there are really only two additions to my chart:

  • If a property doesn’t need to change, it can be a let.
  • @Bindable only works for classes. The equivalent for structs or primitive data types is still @Binding.

[…]

And lastly, in @Observable classes, everything that is NOT private is published. This is the opposite to what we had before where you had to explicItly state which properties were published.

Keith Harrison:

[Observable] has a number of benefits:

  • Simplified data flow, using State and Environment. You no longer need StateObject or EnvironmentObject.
  • Less boiler-plate. No need to annotate properties with @Published or models with @ObservedObject.
  • A view is now only updated when a property it depends on changes not when any change happens to the observable object.
  • You can have arrays of observable models or even observable types that contain other observable types.

Apple has a useful guide on the steps to migrate an ObservableObject to an Observable. To summarise:

Ish Abazz:

In case anyone else runs across this… Using @Observable on a class that has a property that is a closure causes the Xcode beta 3 compiler to flip out. Using @ObservationIgnored on the said property will get the compiler to calm down.

Previously:

Update (2023-07-26): Keith Harrison:

In iOS 17, Apple deprecated the onChange(of:perform) view modifier replacing it with two new variations.

Update (2023-08-10): Natascha Fadeeva:

When it comes to state management in SwiftUI combined with reference types, SwiftUI provides two property wrappers: @StateObject and @ObservedObject. Understanding the difference between them is crucial for building robust SwiftUI applications, as it determines how data is managed and flows through the view hierarchy.

Note: Apple introduced the Observation framework at WWDC23 which will make these two property wrappers obsolete in the future. The knowledge around them is still useful since they are used in many existing applications. The knowledge might also help migrate to new Observation framework.

Update (2023-08-22): Thomas Clement:

Things are getting a bit weird in the swift forums, there’s been a huge amount of feedback in several discussions over several months about missing didSet support in Observability and Apple’s response has been just complete silence.

Update (2024-02-14): Donny Wals:

In this post, we’ll explore the new @Observable macro, we’ll explore how this macro can be used, and how it compares to the old way of doing things with ObservableObject.

Comments RSS · Twitter · Mastodon

Leave a Comment