Questions tagged [observation-framework]

Observation provides a robust, type-safe, and performant implementation of the observer design pattern in Swift.

Observation provides a robust, type-safe, and performant implementation of the observer design pattern in Swift. This pattern allows an observable object to maintain a list of observers and notify them of specific or general state changes. This has the advantages of not directly coupling objects together and allowing implicit distribution of updates across potential multiple observers.

The Observation framework provides the following capabilities:

  • Marking a type as observable
  • Tracking changes within an instance of an observable type
  • Observing and utilizing those changes elsewhere, such as in an app’s user interface

To declare a type as observable, attach the Observable macro to the type declaration. This macro declares and implements conformance to the Observable protocol to the type at compile time.

For more information see WWDC 2023 video Discover Observation in SwiftUI.

2 questions
1
vote
2 answers

Swift Observation framework and debounce

I am currently migrating some SwiftUI code from combine to the new observation framework and I want to know how I can I maintain debounce? Is there anyway to natively debounce from within the Observation framework? I have tried combining Combine ~no…
1
vote
1 answer

Actor isolate @Observable type

Back in WWDC 2021’s Discover concurrency in SwiftUI, they recommend that you isolate the ObservableObject object to the main actor. E.g.: struct ContentView: View { @StateObject var viewModel = ViewModel() var body: some View { …
Rob
  • 415,655
  • 72
  • 787
  • 1,044