What is the best place to call web services in iOS application with MVVMC architecture?

mvvm coordinator swift
coordinator design pattern swift
mvvm project structure ios
coordinator pattern android
swiftui coordinator pattern
mvvm swift
ios flow controller
xcoordinator

Currently I'm developing an iOS application by using MVVMC architecture. I was getting some idea about MVVMC by reading this article . As a typical MVVM model we know all the major app controllers like web service calls should call in the ViewModel class. But in the MVVMC architecture we can user either Coordinator or ViewModel to call web services. I couldn't figure out what's the best place for this.

I'm currently trying to implement user list page of the application using UITableViewController. Following are the some parts of my UserCoordinator and UserViewModel classes.


UserCoordinator

class UsersCoordinator: Coordinator {

var window: UIWindow
weak var delegate: UsersCoordinatorDelegate?

var  selectedCity: City?

init(window: UIWindow) {
    self.window = window
}

func start() {
    let storyboard = UIStoryboard(name: "Users", bundle: nil)
    if let vc = storyboard.instantiateViewController(withIdentifier: "list") as? UsersListController {
        var viewModel = UsersListViewModel()
        viewModel.delegate = self as UsersListViewModelDelegate
        viewModel.veiwController = vc
        vc.viewModel = viewModel
        vc.coordinationDelegate = self as CoordinationDelegate
        let nav = UINavigationController.init(rootViewController: vc)
        window.rootViewController = nav
    }

}

UserViewModel

 protocol UsersListViewModelDelegate: class  {
    func selectUser(viewController: UIViewController, city: City)
}

struct UsersListViewModel {
    var delegate: UsersListViewModelDelegate?
    weak var veiwController: UsersListController!
    var source = [String]()

    init() {
        for user in users {
            source.append(user.name)
        }
    }

    func selectRow(row: NSInteger) {
        delegate?.selectUser(viewController: veiwController, user: users[row])
    }

    fileprivate var users: [User] {
        get {
            //web service call??
        }

Where should I call the web service here? As I have read theoretically Coordinator is the dedicated place for application routing. So according to that it's better to call web services in ViewModel. But I feel it's better to call web services in the Coordinator because it'll load data very quickly and populate the viewModel. What should I do?

You need to understand the purpose of ViewModel and Co-ordinator to know what should contains what. The responsibility of a ViewModel is to prepare data for the View and handles the business logic for preparing that data. It binds data to UI elements and changes UI as soon as data changes. So ViewModel should be the one which should contains the service and all calls should be made from it.

The responsibility of co-ordinator is to handles the navigation of application by showing the appropriate ViewController with all it's dependencies which includes the ViewModel. So it also prepares the ViewModel and injects the service in it not use that service to manipulate data.

So if I summarise, Co-ordinator should be the one who prepares a ViewModel with it's dependencies which includes the injecting webservice in it and ViewModel should be the one who use that service and makes all the call for getting data from the persistence .

For more clear idea you can check my sample application on MVVM-C at my Github

Coordinator Tutorial for iOS: Getting Started, MVVM + Coordinators + RxSwift and sample iOS application with For code samples from this article check out this repository: Swift-MVVMC-SimpleExample is nothing wrong in my opinion to call some part of the Model as Services. for small apps where you can start up with a simple architecture and  2 What is the best place to call web services in iOS application with MVVMC architecture? Oct 25 '17 2 Accessing the value of a particular key in firebase using swift Oct 27 '17

As I researched through the internet there is no convention where to invoke your web services. It's totally depend on your project implementation. Either you can invoke them on your Coordinator or ViewModel class. Since we are using Coordinator to initialize

  1. ViewController
  2. ViewModel

Inside the Coordinator class as I have posted above. We can use same start() function to invoke the web services as following.

func start() {
    let storyboard = UIStoryboard(name: "Users", bundle: nil)
    if let vc = storyboard.instantiateViewController(withIdentifier: "list") as? UsersListController {
        let uDs = UserDataService(pClient:APIClient())
        uDs.getUserDetails { (response, status) in
            if (status == APIClient.APIResponseStatus.Success) {
                if let resArrray = response as? [User] {
                    var viewModel = UsersListViewModel.init(userList: resArrray)
                    viewModel.delegate = self as UsersListViewModelDelegate
                    viewModel.veiwController = vc
                    vc.viewModel = viewModel
                    vc.coordinationDelegate = self as CoordinationDelegate
                    let nav = UINavigationController.init(rootViewController: vc)
                    self.window.rootViewController = nav
                }
            } else {
                //error alert
            }
        }
    }
}

It's better to invoke web service calls inside the Coordinator class. Because after invokes the web service according to the response we can controll the view navigation. Such like if response get success we could create a ViewController class and load it. Otherwise we could load an error popup. Meantime we could take enough time to the web service call and populate those data to the viewModel.

Based on what I have found through the internet , I have implemented a sample project using MVVM-C architecture. I have hosted it in GitHub. You can find it in here.

By refering this poroject you can have a clear idea about to how to implement view navigations using MVVM-C pattern, How to invoke web services, How to populate viewModels from data etc.

MVVM + Coordinators + RxSwift and sample iOS application with , I don't think whole app using same design pattern makes sense for all cases. It's better that view controller's prepareForSegue is still called. So we can A MVVMC structure could be designed as following. Languages I've Learned, or Not — Web Forward Welcome to a place where words matter. 4 What is the best place to call web services in iOS application with MVVMC architecture? Oct 30 '17 4 how do I loop through an array assign a variable to each item in Objective C Jun 4 '19

When it comes to architecture, there's no silver bullet. Keeping that in mind MVVM-C is one of the most clean, easily testable and modular pattern, so it's great that you are going ahead with it.

You are free to customize the architecture to suit your requirements. Having said that, considering your current design I think it would be best to create a Service class, call it WebService. This class will perform networking operations under the supervision of the Coordinator. The Coordinator will

  1. Init WebService
  2. Perform network calls
  3. Init the ViewModel with the fetched data (or instruct the ViewModel to handle a failed network call)

MVVMC with storyboard in iOS - Alex Hsieh, You call the REST API and get back raw json object. Service: The place where the network request/calls are constructed and initiated. As an iOS developer, you often need to use a web service from your app. Sometimes you need to use a web service that someone else has written, and sometimes you need to use one of your own!

iOS App Architecture - Karthikkeyan Bala Sundaram, Defining a good and clean architecture with helpful names and nice design MVVM worked so well in Android projects that we decided to use it in iOS projects too. MVVMC. ViewModel is not a new concept. It's the main part of the NET applications, but of course we can use it anywhere – including  In many respects, it is better to compare the web frontend of an app to an iOS or Android client, which is both a service‑based client and a rich application unto itself. Our approach to building a web frontend combines the best of web application design with microservices philosophy, to provide a rich user experience that is service‑based

MVVM in iOS, This tutorial takes a deep look at architectural patterns, analyzing their benefits to help you create a robust, easy-to-test mobile iOS application with Swift. With MVP, you have a new layer called Presenter which contains the you may have some edge-cases where MVVM-C may be impossible to use—or  Best practice: Adopt and embrace DevOps practices. Before the emergence of DevOps as an application development methodology, on-call duties typically rested on a subset of engineers and other IT personnel, such as a centralized site reliability or operations team.

MVVM-C With Swift, Nowadays, the biggest challenge for an iOS developer is the craft of a Architectural patterns are similar to software design pattern but have a broader scope. Database and so on), contains the business logic of your application you may have some edge-cases where MVVM-C may be impossible to  Although SOAP APIs are stateless by default, SOAP does support stateful operations that can be implemented using the WS-* (Web Services) Specifications that are built on top of the core XML and SOAP standards. SOAP vs. REST comparison table. Although REST is very popular these days, SOAP still has its place in the world of web services.

Comments
  • I think this should be preferred since it has decoupling benefits and also makes it easier to write tests for other layers
  • Yes. I already created a service class ,looking where to invoke that methods. As you said I also feel invoke in the coordinator is better. Thank you very much.