title: Proper way of setting delegates in MVVM
body: <p>I would like to know what is a proper way of setting delegates in the ViewModel in MVVM pattern in Swift.</p>
  
  <p>I'm instantiating the ViewController from another class:</p>
  
  <pre><code>let viewModel = DashboardViewModel()
  let viewController = DashboardViewController(viewModel: viewModel)
  </code></pre>
  
  <p>My ViewModel:</p>
  
  <pre><code>protocol DashboardViewModelType {
      var items: [Item] { get }
      var reloadDelegate: DashboardDataReloadDelegate? { get set }
  }
  
  protocol DashboardDataReloadDelegate: class {
      func reloadData()
  }
  
  class DashboardViewModel: DashboardViewModelType {
  
      var items: [Item] = []
  
      weak var reloadDelegate: DashboardDataReloadDelegate?
  
      init() {
          loadItems()
      }
  
      func loadItems() {
          let databaseFetcher = DatabaseDaysFetcher()
          databaseFetcher.getDays(onData: { (items) in
              self.items = items
              reloadDelegate?.reloadData() //delegate is nil here
          }) { (error) in
              print(error)
          }
      }
  }
  </code></pre>
  
  <p>and ViewController:</p>
  
  <pre><code>class DashboardViewController: UIViewController {
  
      var viewModel: DashboardViewModelType?
  
      init(viewModel: DashboardViewModelType) {
          self.viewModel = viewModel
          super.init(nibName: nil, bundle: nil)
          self.viewModel!.reloadDelegate = self // it is executed after 
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
      override func viewDidLoad() {
          super.viewDidLoad() 
      }
  
  }
  
  extension DashboardViewController: DashboardDataReloadDelegate {
      func reloadData() {
          print("data reloaded")
      }
  }
  </code></pre>
  
  <p>So the main problem is that if I want to inject the viewModel in another class I'm instantiating the viewModel when delegate is not yet set. Would it be better to declare <code>loadItems</code> inside the <code>DashboardViewModelType</code> protocol and then call this function from the init or viewDidLoad inside the ViewController?</p>