Post

Replies

Boosts

Views

Activity

Comment on Observation and MainActor
@erdeszbalazs read that link closely, it says "in your data model type", it isn't for view data, that is what View structs are designed for. Also at 20:50 in Data Essentials in SwiftUI WWDC 2020 they say "Views are very cheap, we encourage you to make them your primary encapsulation mechanism".
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Comment on Observation and MainActor
You shouldn't do @State private var vm = ViewModel() because your ViewModel is a class. You would be better using the View as the view model and declaring @State for your data, e.g. @State var showDetails: Bool = false. You could group related state vars into a struct if you like but not a class. You can use mutating func for logic related to the vars.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Comment on Stop using MVVM for SwiftUI
When it detects a change in data, SwiftUI makes new View structs and diffs then with the previous ones. That difference is used to drive initing/updating/deallocing UIKit objects. Hopefully that helps you understand View structs are the view model.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’24
Comment on Stop using MVVM for SwiftUI
If it doesn't make sense the first thing to learn (and Apple don't do a great job of explaining this) that the View struct hierarchy with its dependency tracking and diffing is the view model already. If you ignore that and use your own view model objects then you'll likely have the same consistency bugs that SwiftUIs implementation using structs was designed to eliminate. It's very tempting to use familiar objects but it really is worth putting the effort in and learning to use View structs.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’24
Comment on When SwiftUI View is Equatable - questions...
edit: regarding the last question, sorry in my testing View was still Equatable. Once I removed that, the object's == was called and if I had 2 objects and switched them but made the == make them look equal then body wasn't called. ContentView2(object: ((counter % 2 == 0) ? object1 : object2), x: counter2). This probably isn't too useful for ObservedObject but for standard objects that are designed to be immutable, like NSURL or NSManagedObjectID then I suppose this behaviour is useful.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’23