Is @ObservationIgnored necessary for private var?

In SwiftUI's ViewModel class that are @Observable, is it necessary to annotate private fields as @ObservationIgnored?

I'm not sure if adding @ObservationIgnored to these fields will get performance gains, since there are no SwiftUI structs referencing these fields because they're private. However, I'd like to know what's the recommended approach here?

While this might not seem obvious for the example below, however, sometimes I have private fields that are changing pretty frequently. For these frequently changed fields, I think the performance gains will be larger.

Example:

@Observable 
class UserProfileViewModel {
  var userName: String?
  var userPhoneNumber: String?
  
  private var isFetchingData = false
}

vs

@Observable 
class UserProfileViewModel {
  var userName: String?
  var userPhoneNumber: String?
  
  @ObservationIgnored private var isFetchingData = false
}

As it turns out, the @ObservationTracked macro is automatically granted even if it is a private var. Therefore, I think it is not wrong to grant @ObservationIgnored. @ObservationTracked grants the following processing to isFetchingData.

private var isFetchingData = false
// Code given by ObservationTracked macro from here on
{
    @storageRestrictions(initializes: _isFetchingData)
    init(initialValue) {
        _isFetchingData = initialValue
    }
    get {
        access(keyPath: \.isFetchingData)
        return _isFetchingData
    }
    set {
        withMutation(keyPath: \.isFetchingData) {
            _isFetchingData = newValue
        }
    }
    _modify {
        access(keyPath: \.isFetchingData)
        _$observationRegistrar.willSet(self, keyPath: \.isFetchingData)
        defer {
            _$observationRegistrar.didSet(self, keyPath: \.isFetchingData)
        }
        yield &_isFetchingData
    }
}

For the use you are describing, it is conceivable that the set and _modify would be called if the data is changed frequently, which would slow down the process. However, I'm sorry to say that I don't know if you can really feel it. Personally, I don't see the point of running this process with private, so I would add @ObservationIgnored to avoid wasting code.

If you want to know more about this Observation macro, you may want to take a look at the following implementation in Swift.

GitHub Swift Language Repository

Only properties that are referenced in view and "registered" are accessed. From my experience it makes sense to add ObservationIgnored when you as a programmer really don't want that property to be tracked by view by error.

Is @ObservationIgnored necessary for private var?
 
 
Q