MVVM in iOS — View Model and Networking

In my previous article I pointed out that your view model should be as lean and dumb as possible. This means your view model should not perform data access or networking tasks. Few readers pointed out that Microsoft, where MVVM pattern has been used for ages advocates the concept of calling network and data access layer through the view model.

In this post I would like to take the Microsoft approach and move the networking code inside the view model implementation. I have updated the definition of ArticleListViewModel to the following:

ArticleListViewModel is now a class and requires an instance of Webservice to be initialized. Ignore the initializer which takes the articles as a parameter for now, since it was used in the previous post.

ArticleListViewModel consists of a get function which is responsible for returning the populated view model to the caller.

Instead of returning a list of ArticleViewModel objects I am returning a single ArticleListViewModel object which populates all the child objects.

Now, inside the view controller you can call the view model’s get method as shown below:

The complexity of calling and converting domain objects to the corresponding view model objects is moved from the view controller to the view model implementation.

If you remember my original definition of view models, a view model is responsible for supplying the data to the view. This new approach is certainly doing that but at the expense of making the view model complicated.

There is no right or wrong approach. MVVM comes in different shapes and forms and implementations. When working on your project, take the approach that best fits your needs.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.