The Observation Framework

Helder Pinhal
Helder Pinhal
Jul 14 2023
Posted in Engineering & Technology

Propel your SwiftUI app's performance to new heights.

The Observation Framework

As we find ourselves in this exciting time of the year, reflecting on last month's WWDC event becomes inevitable. Apple, known for its innovative offerings, has once again captivated our attention with a host of exciting updates. In this article, we delve into the remarkable Observation framework, exploring the multitude of advantages it brings to the table.

Exploring the advantages of the Observation framework

The Observation framework offers a range of benefits that greatly enhance the development experience. Let's delve into some of its key advantages:

  1. Enhanced Tracking Capabilities
    One significant advantage of the Observation framework is its ability to track both optionals and collections of objects. This feature sets it apart from the traditional ObservableObject approach. By leveraging the Observation framework, developers can effortlessly monitor changes within these entities, enabling more precise control and efficient handling of data.

  2. Data Flow Primitives
    The Observation framework provides new data flow primitives such as State and Environment. These primitives eliminate the need for object-based equivalents like StateObject and EnvironmentObject. By leveraging familiar primitives, developers can leverage a more streamlined and cohesive code structure, ultimately improving code readability and maintainability.

  3. Performance Optimization
    Another notable advantage of the Observation framework lies in its ability to update views based specifically on changes to observable properties that a view's body reads. This approach contrasts with the ObservableObject method, which triggers updates upon any property changes occurring within the observable object. By narrowing the focus to relevant changes, the Observation framework enhances app performance, leading to smoother user experiences and optimized resource utilization.

The Observation framework introduces a powerful set of benefits that positively impact the development process. From improved tracking capabilities to seamless integration with data flow primitives and performance optimization, embracing this framework unlocks new avenues for efficient and robust app development.

Getting to know the changes

Migrating to the new Observation framework is a hassle-free process that brings remarkable simplicity to your codebase. By replacing a few object-based property wrappers with the convenient macros provided by the Observation framework, you can streamline your code and improve its readability.

Start by importing the Observation framework and replace the usage of ObservableObject with the Observable macro. This simple substitution ensures that observation support is added to your type, generating the required source code at compile time.

// BEFORE
import SwiftUI


class Library: ObservableObject {
    // ...
}
// AFTER
import SwiftUI
import Observation

@Observable class Library {
    // ...
}

Unlike the ObservableObject approach, the Observation framework eliminates the need for the Published property wrapper to make properties observable. Remove the Published property wrapper from your observable properties, as Observation determines observability based on the property's accessibility in relation to an observer (such as a view).

// BEFORE
@Observable class Library {
    @Published var books: [Book] = [Book(), Book(), Book()]
}
// AFTER
@Observable class Library {
    var books: [Book] = [Book(), Book(), Book()]
}

Incremental Adoption of Observation Framework

Adopting the Observation framework in your app doesn't require a complete overhaul of the ObservableObject protocol across your entire codebase. Instead, you have the flexibility to make incremental changes, gradually integrating the new framework into your existing setup. By starting with a single data model type and leveraging the Observable macro, you can seamlessly introduce the Observation framework without disrupting the rest of your app.

One of the key advantages of the Observation framework is its compatibility with a mixed usage of data model types employing different observation systems. This means that within your app, you can have a combination of data model types using both Observable and ObservableObject approaches. However, it's important to note that SwiftUI tracks changes differently based on the observation system utilized by each data model type. This divergence in change tracking ensures that the framework adapts to the observation system employed by the specific data model type, be it Observable or ObservableObject.

By embracing this incremental approach, you can smoothly transition your app to the Observation framework, introducing its benefits step by step. This flexibility allows for a seamless integration with your existing codebase, empowering you to leverage the Observation framework's advantages without disrupting your app's functionality or causing unnecessary complexity.

Conclusion

In conclusion, the Observation framework marks a significant milestone in the evolution of Swift. Its introduction brings notable benefits and addresses various challenges that developers encountered in the past. With simplified macros for state management, the framework eliminates confusion and streamlines the development process.

Moreover, the Observation framework significantly enhances app performance. By minimizing the computational load required to process view changes, it optimizes resource utilization and promotes smoother user experiences. This performance boost is a valuable asset for developers striving to create high-performing applications.

As always, we hope you liked this article, and if you have anything to add, we are available via our Support Channel.

Keep up-to-date with the latest news