[Second review] SE-0395: Observability

This hybrid solution seems to be working † indeed:

import SwiftUI
import Observation

@Observable class Model: ObservableObject {
    static let shared = Model()
    var relevant = 0
    var irrelevant = 0
    init() {
        Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
            self.relevant += 1
        }
        Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in
            self.irrelevant += 1
        }
    }
}

struct ContentView: View {
    @ObservedObject private var model = Model.shared
    
    var body: some View {
        let _ = print("body called")
        withObservationTracking {
            VStack {
                Text(String(model.relevant)).font(.largeTitle)
            }
        } onChange: {
            DispatchQueue.main.async {
                model.objectWillChange.send()
            }
        }
    }
}

@main struct newApp: App {
    var body: some Scene {
        WindowGroup { ContentView() }
    }
}

Could be another way how to approach back deployment.

† although to truly test it I need to somehow disable the new machinery in this line:

 @ObservedObject private var model = Model.shared
3 Likes