Skip to content

It's not ViewModel specific, make it a generic "@ObservedObject" #17

@helje5

Description

@helje5

As discussed in Slack:

I guess my question is why this is called ViewModel instead of (UI?)ObservedObject 🙂

UIObservedObject could work I guess (and would sidestep the “what’s the difference between a model and a view model” question), but I plan to add AppKit support and it didn’t make sense to tie the naming to UIKit

I'm by no means an expert in MVVM, but to me a View Model is a wrapper around a model extracting just the required parts being displayed. And usually (maybe not necessarily) read-only i.e. a struct/value type.

So I was kinda expecting some machinery which can map a model (the observable object) to a viewmodel (the view representation of it).

If you are not going to mix w/ SwiftUI, just @ObservedObject seems decent to me

There is really nothing MVVC specific to it, right?

For MVVC my feeling is that the model should be the observable object, the VM a map of that to a struct on changes, which somehow gets propagated to the VC

I'd call it ObservedObject, because this is what it does. instead of calling body, the UI/AppKit version calls that updateView, which is perfectly in line IMO

That’s a reasonable argument. It’s not in the documentation but this is also a good pattern for migrating code towards SwiftUI, because these objects are already SwiftUI-compatible
So I think that might have been why I was avoiding that name, because it felt “taken” by SwiftUI

It's probably a StateObject in SwiftUI 2 terms 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions