Hi,
Previously, we would conform model objects to the ObservableObject
protocol and use the @StateObject
property wrapper when storing them to an owned binding in a View.
Now, if I understand correctly, it is recommended that we use the new @Observable
macro/protocol in place of ObservableObject
and use the @State
property wrapper rather than @StateObject
. This is my understanding from documentation articles such as Migrating from the Observable Object protocol to the Observable macro.
However, the StateObject
property wrapper has an initialiser which takes an autoclosure parameter:
extension StateObject {
public init(wrappedValue thunk: @autoclosure @escaping () -> ObjectType)
}
This is an extremely important initialiser for state objects that are expensive to allocate. As far as I can tell, the @State
property wrapper lacks an equivalent initialiser.
What is the recommended migration strategy for objects which made use of this on StateObject
?
Thanks