Using "withObservationTracking" on a property from within the class can break any dependent SwiftUI views

Originator:patinanutshell
Number:rdar://FB13556571 Date Originated:01/22/2024
Status:open Resolved:no
Product:iOS Product Version:17.2
Classification:Bug Reproducible:Yes
 
Description

Using "withObservationTracking" while referencing one of the properties within the same class completely breaks SwiftUI views that use this observable instance.

Here's a simple example that breaks, where observing the property "count" breaks any SwiftUI View that might be tracking the same property. Attached a sample to reproduce, where tapping `Increment` has no effect until you comment out the function invoking "withObservationTracking". 

The view then behaves properly from that point on.

@Observable class Test {
  var count: Int = 0
  init() {
     withObservationTracking { _ = self.count }
     onChange: { [weak self] in print("Count: \(self?.count ?? 0)") }
  }
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!