In this video, I am going to show you different implementation when you move @State property from a view to a ObservableObject class.
The advantage of properties in view models is that you can add more business logic with the Combine framework. @Published is important because it will redraw the view whenever the value changes. This is working by calling objectWillChange.send() in the willSet property observer.
But you get a publisher for your property too.
You will see how to create a data stream that saves the property to UserDefaults. UserDefault has some sophisticated mechanism when it actually saves the value. It has some resource management that will save the newest value every 10 sec or so. I am using debounce, to create a similar behaviour. Therefor, in practice you will not need to use debounce with UserDefaults, but I thought it was a nice demonstration. If you switch to save in the file system, this is going to be useful.
⬇️ you can find the image here: github.com/gahntpo/swiftui-pr...
If you want to check out the other videos that I made about property wrappers in SwiftUI you can go here:
Property wrappers in SwiftUI • Property wrappers in S...
If you liked what you learned and you want to see more, check out one of my courses!
👨💻 my SwiftUI course school.swiftyplace.com/course...
👨💻 my Core Data and SwiftUI course school.swiftyplace.com/course...
👩🏻💻 Combine course school.swiftyplace.com/course...
#SwiftUI #Xcode #iOS
Негізгі бет @Published vs @State - SwiftUI by example - using Combine to save in UserDefaults
No video
Пікірлер: 14