A property wrapper type that instantiates an observable object.
Create a state realm object in a
SwiftUI/Scene by applying the
@StateRealmObject attribute to a property
declaration and providing an initial value that conforms to the
@StateRealmObject var model = DataModel()
SwiftUI creates a new instance of the object only once for each instance of the structure that declares the object. When published properties of the observable realm object change, SwiftUI updates the parts of any view that depend on those properties. If unmanaged, the property will be read from the object itself, otherwise, it will be read from the underlying Realm. Changes to the value will update the view asynchronously:
Text(model.title) // Updates the view any time `title` changes.
You can pass the state object into a property that has the
SwiftUI/Binding to one of the state object’s properties using the
$ operator. Use a binding when you want to create a two-way connection to
one of the object’s properties. For example, you can let a
SwiftUI/Toggle control a Boolean value called
isEnabled stored in the
Toggle("Enabled", isOn: $model.isEnabled)
This will write the modified
isEnabled property to the
model object’s Realm.