A property wrapper type that creates an ObservableObject
for just a single property.
This can have enormous performance benefits due to how SwiftUI recomputes views.
Views get called to update each time any of an object's properties change. Even if it's irrelevant to a given view.
Consider the following common pattern of a typical custom view with one @State
property.
struct BedroomLabel: View {
@State var areTheLightsOn = false
var numberOfPeople = 0
var body: some View {
VStack {
Text("This bedroom has \(numberOfPeople) people in it.")
Toggle("Lights On/Off", isOn: $areTheLightsOn)
}
}
}
This is a very common pattern in SwiftUI, and it works alright, but the BedroomLabel
will update in its entirety whenever the lights get toggled. This means the CPU spends unnecessary time checking the entire body whenever this happens.
How can we do better?
Here is that same example written using Watchable
.
struct BedroomLabel: View {
@Watchable var areTheLightsOn = false
var numberOfPeople = 0
var body: some View {
VStack {
Text("This bedroom has \(numberOfPeople) people in it.")
Watching(bindingOf: $areTheLightsOn) { isOn in
Toggle("Lights On/Off", isOn: isOn)
}
}
}
}
@Watchable
writes just like @State
, except it stores the value in an implicit generic ObservableObject
type. Watching
takes care of unwrapping that and recomputing a view closure whenever its changed.
This means BedroomLabel
won't be computed on changes. Only the Toggle
will be.
Use Swift Package Manager with this GitHub URL.
MIT License
Copyright © 2021-2024 Nicolas Cook Leon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.