Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
helje5 opened this issue Aug 26, 2020 · 3 comments
Open

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

helje5 opened this issue Aug 26, 2020 · 3 comments

Comments

@helje5
Copy link

helje5 commented Aug 26, 2020

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 🙂

@sharplet
Copy link
Contributor

This is kind of disappointing — I tried renaming to ObservedObject, and this is the compiler error when importing both CombineViewModel and SwiftUI:

Sources/App/ProfileViewController.swift:7:3: error: unknown attribute 'ObservedObject'
  @ObservedObject var viewModel: ProfileViewModel
  ^

@sharplet
Copy link
Contributor

Qualifying with the module name still works, it just that this diagnostic is not helpful at all.

@sharplet
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants