Riccardo Cipolleschi
2 min readDec 1, 2021

--

Thanks again for taking the time for writing your thoughts.

I think now I understand better your point... and I agree with it to some degree.

There is still one use case that I can't fit in your reasoning.

If I got your point correctly, whenever we have an ObservableObject, we basically have a ViewModel, whatever we call it.

And we agree that a ViewModel is something that is bound to a specific view, so it makes no sense to extract it to a protocol.

But, what if we have different sections of the app that has to listen for a shared piece of state? In that case, we need an external dependency that @Publishes the shared piece of state, and that is an ObservableObject.

But in that case, it is not a ViewModel. It is some sort of storage with behaviours that keeps track of some state and notifies the rest of the application about that. Then, other ViewModels will be recreated by SwiftUI and can adapt the new information to what the View needs (To be honest, I'm about to publish an article about this use case in the next few days).

After writing this down, I reached the conclusion that ObservableObject may have a wider range of use cases than simply ViewModel. What do you think? At the end of the day, the property published by a Service/Manager/Store could be listened to by other pieces of logic (that have no relationships with SwiftUI) so it won't be a ViewModel at all.

I'd like to address another point you raised that is interesting:

> In all but the very simplest of cases, that service is going to have functionality and behavior that, if the service is simply swapped out, will not be tested under those conditions.

It is true, but in a View we should pass a dependency with a smaller interface. The View should only know about the behaviours it cares about. So it's fine that other Service behaviours are not tested when it is swapped.

The production Service will have its own unit tests to check that the production implementation behaves correctly. Do you agree?

If that's the case, we shouldn't care about the other behaviour of the Service in the View.

I really like the discussion we are having so far. I also feel that medium comments are not really a good medium to carry it on.

If you like, feel free to write to me at: riccardo.cipolleschi@gmail.com or on LinkedIn!

Thanks again for the interesting thoughts.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Riccardo Cipolleschi
Riccardo Cipolleschi

Written by Riccardo Cipolleschi

Hey there, I’m Riccardo. Software engineer at Meta. I have a passion for iOS and I love to share my knowledge with others.

No responses yet

Write a response