thoughtbot / CombineViewModel

An implementation of the Model-View-ViewModel (MVVM) pattern using Combine.
MIT License
59 stars 4 forks source link

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

Open helje5 opened 4 years ago

helje5 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

sharplet commented 4 years ago

Raised https://bugs.swift.org/browse/SR-13470.