@Observable and didSet
When you mark a class as @Observable you can no longer use didSet because the macro converts it to a computed property, e.g. a simple: When expanded becomes: As you can see test has become a computed property that is Read more
When you mark a class as @Observable you can no longer use didSet because the macro converts it to a computed property, e.g. a simple: When expanded becomes: As you can see test has become a computed property that is Read more
The built-in @Query does not currently support dynamic changes, instead we can use @DynamicQuery from the SwiftDataX open-source package to achieve it. The query declaration looks like this: By default that will load all Prospect model objects, sorting them by name. If Read more
SwiftUI’s design takes advantage of equality to decide if things should be updated. Measurement (backed by NSMeasurement) has something interesting in its Equatable implementation: This means that 0kg is equal to 0g despite their unit being different. This makes sense Read more
Reposted from: https://forums.swift.org/t/how-to-use-observation-to-actually-observe-changes-to-a-property/67591/18 I thought I’d share an example of how I am currently using it. I wanted to make an object that maintains a sorted order of the main model’s objects. I used withObservationTracking to update if either the sort order Read more
I came across this StackOverflow question How do I access the SwiftData container from outside of SwiftUI? but it turns out they needed to use the container inside an AppIntent. I knew the way to inject objects into AppIntents is Read more
Health warning: In general I would not recommend this approach. View data should be @State simple values or custom structs with mutating func for testable logic, passed down as let for read only, computed vars for transforming, or @Binding var Read more
If you attempt to init another instance of CLMonitor with the same name it crashes with: Since I wanted to use the same monitor from different tasks I needed to turn it into a singleton and came up with this: Read more
Following on from my previous post on this topic I found a forum post with someone struggling to build a calculator using @Observable and thought maybe my technique of wrapping withObservationTracking in AsyncStream might help and came up with the Read more
I needed to create an AsyncStream to monitor changes to an @Observable model class’s var and by using withCheckedContinuation and AsyncStream‘s unfolding init I came up with this: The challenge here is withObservationTracking only watches for a single change but Read more
I noticed a snag when using NavigationLink(value:label:) and .navigationDestination(for:destination:) with the new @Observable vs the old Combine ObservableObject (which “just worked™️”). There are 2 compilation errors: Fortunately it is relatively simple to add Hashable conformance to an @Observable class by Read more