@Published vs @State - SwiftUI by example - using Combine to save in UserDefaults
Вставка
- Опубліковано 22 сер 2024
- 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/gah...
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.swiftyp...
👨💻 my Core Data and SwiftUI course school.swiftyp...
👩🏻💻 Combine course school.swiftyp...
#SwiftUI #Xcode #iOS
Excellent tutorial! I definitely need to be using combine debounce and dropFirst more
Very good indeed
Amazing! Thank you :)
I am very interested in seeing more videos about combine from you. Thank you
We should get more information about Combine framework from you. Thanks for the video.
Very good tutorial that combines Combine and AppStorage
OMG this channel is revelation. Since SwiftUI is yet new and you have put it together so simply! where is your patreon?
Hey there, best tutorials I’ve seen in a while! Thanks a lot!
I got one question: if you use debounce in a subscription that’s used to store values and as you said may schedule only every 5 or 10 seconds, what happens if you quit the view/application within that time?
Will sink get called one last time or will I store an „old“ value?
Great tutorial, please more combine please
Thank you for the tutorial!!
Just found out about your videos today and I liked them so far. Especially the ones with combine.
Would be great to get some more content there. Maybe something which combines access and publishing values over time would be great. I think about location access & user location or maybe something with health (access steps or so). This would be a little more advanced stuff, but could also show a bit of error handling
As a short note to this video: @StateObject is also not available in iOS 13.
Thank you so much. I just started learning swift ui as hobby for a calculator I'm doing and I needed to transfer some data between two different views. This tutorial made it work !
The problem I encountered was when creating the class as observable in one file as you did here in a separate file, then in the main view I have the calculations user interface ( until here all works when I work in the main view using @StateObject), but then in the secondary view using @ObservedObject, I needed the data to be represented like in a table shape but the new values were never updated. It was only taken the default values when I created the class.
I'd like to know if this is the only way to sort this out or there is an easiest way.
Thanks again!
Another great explanation. I'm still searching for the merits of such a system vs encoding to Json? What's your perspective? TIA
Thank you for the tutorial Karin. I'm following you course on Udemy.
What I'm trying to solve using SwiftUI - CoreData - CloudKit and the MVVM Architecture.
When using the boiler plate code for CoreData data syncs via iCloud to my other devices.
With my MVVM architecture implemented data is updated and sync to iCloud. But the UI on the other device are synced
I've set the container.viewContext.mergePolicy