mercredi 25 octobre 2017

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

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?

Aucun commentaire:

Enregistrer un commentaire