Comparing @Observable to ObservableObjects in SwiftUI

  Рет қаралды 1,600

donny wals

donny wals

Күн бұрын

Пікірлер: 14
@andresraigoza2082
@andresraigoza2082 7 ай бұрын
I found very useful the idea of thinking that @State is telling SwiftUI who owns that object. I'll never forget that, thank you so much!
@larschristiansen3136
@larschristiansen3136 7 ай бұрын
Thank you for this video. The reason why an object can sometimes be a let and not a var has puzzled me, but thanks to your video I now understand why.
@DonnyWalsdev
@DonnyWalsdev 7 ай бұрын
Thanks! So happy the video was able to clear this up. I think it's pretty much one of the major things people trip over with the new macro
@w0mblemania
@w0mblemania 7 ай бұрын
This was very useful. Thanks.
@DonnyWalsdev
@DonnyWalsdev 7 ай бұрын
Thank you!
@JosephHeck
@JosephHeck 7 ай бұрын
Lovely video overview, although your use of "we" and "you" when describing ownership and what's acting on what in comparing the differences was a bit confusing in places. I'm not yet using @Observable myself, more because of the brutal hit to compile time with macros, but I also want a shared observation publisher setup in a couple of places, which (as you mentioned near the end) Observable doesn't have anything to help with.
@DonnyWalsdev
@DonnyWalsdev 7 ай бұрын
Thanks! Would you say that either we or you works better, and is there one that would be best? Or do you feel like I mixed them up in a confusing way but using both in a video is okay?
@JosephHeck
@JosephHeck 7 ай бұрын
@@DonnyWalsdev I'd suggest not using either - that's what I found confusing. You started with 'you', talking about ownership and @State and how that transfers things, and clarified it a bit later that the view "owned" that variable. It would have been more clear if you started off with "your code" owning that, instead of "you" and then switched to clarify that with @state, the view "owns" the reference and manages its lifecycle. That's one of the key differences in the new semi-defunct ObservableObject vs StateObject - you touch on it well with the newest Observation bits, but it was just confusing getting there.
@DonnyWalsdev
@DonnyWalsdev 7 ай бұрын
@@JosephHeckThanks! That makes a lot of sense :) I appreciate that you took the time to explain what you meant, I'll pay attention to that going forward!
@aalecaar
@aalecaar 7 ай бұрын
Thank you so much for this video, Donny! I have a question. You said we should use @State if the view owns the @Observable object. However, the following code works perfectly fine, even tough it was not marked with @State: @Observable class MyCounter { var count = 0 } struct ContentView: View { private let counter = MyCounter() var body: some View { VStack { Button(action: { counter.count += 1 }, label: { Text("Increment") }) Text("The counter is \(counter.count)") } } } You gave a great and clear explanation about the fact that we use @State if the object is within a view that is itself a subview, and we want to persist its value if the parent view updates. But let’s suppose we are sure the view from the example above won’t be a nested view. We also know that this view owns the object. But given the fact that everything works normally without using @State, is there really a good reason to use it besides the case when the object is in a subview and we don’t want to lose its value if a parent view updates? And thank you again for your video, it helps a lot.
@DonnyWalsdev
@DonnyWalsdev 7 ай бұрын
Thanks for watching! Yes, the good reason here IMO is semantics. Using @Stare will clearly mark the ownership and intent and will help prevent problems in the future if your view does get reinitialized because something changed over time. You’re absolutely right that your example will work if you know the view never gets reinitialized but we should always assume that our views might need to be reinitialized and make sure that our code handles that correctly
@aalecaar
@aalecaar 7 ай бұрын
@@DonnyWalsdev Semantics makes perfect sense. Seems like a good practice to follow. It's like the same reason why you make private the things that don't need to be exposed. I appreciate your time and patience:)
@alperenunal3975
@alperenunal3975 7 ай бұрын
Hey, nice video 🎉 should i use @observationIgnored property wrapper for non tracked properties in observable macro object ? Is observable macro handles itself ?
@DonnyWalsdev
@DonnyWalsdev 7 ай бұрын
You only need ObservationIgnored if you want to be able to use a property in a view and assign values to it without having the view redraw. If no view uses the property at all then the views won’t update when you assign to a property that isn’t ObservationIgnored
Adding haptic effects - Cupcake Corner SwiftUI Tutorial 5/9
15:10
Paul Hudson
Рет қаралды 1,9 М.
the balloon deflated while it was flying #tiktok
00:19
Анастасия Тарасова
Рет қаралды 31 МЛН
What's in the clown's bag? #clown #angel #bunnypolice
00:19
超人夫妇
Рет қаралды 33 МЛН
didn't manage to catch the ball #tiktok
00:19
Анастасия Тарасова
Рет қаралды 35 МЛН
Family Love #funny #sigma
00:16
CRAZY GREAPA
Рет қаралды 3 МЛН
8. SwiftData   CloudKit
18:20
Stewart Lynch
Рет қаралды 8 М.
Binding vs. Bindable in SwiftUI on iOS 17
13:12
donny wals
Рет қаралды 10 М.
SwiftUI: New Observation Framework
8:25
Mike Mikina
Рет қаралды 2 М.
How to determine where code runs in Swift Concurrency
12:42
donny wals
Рет қаралды 2,9 М.
SwiftUI Data Flow in iOS 17 - Observation & @Observable
7:57
Sean Allen
Рет қаралды 54 М.
WWDC23: Discover Observation in SwiftUI | Apple
12:52
Apple Developer
Рет қаралды 3,7 М.
Your Brain 🧠 on Swift Concurrency - iOS Conf SG 2023
30:38
iOS Conf SG
Рет қаралды 10 М.
Stop using Spacer() in SwiftUI
6:02
Flo writes Code
Рет қаралды 13 М.
the balloon deflated while it was flying #tiktok
00:19
Анастасия Тарасова
Рет қаралды 31 МЛН