title: How can I get a list of repositories in the view model before the table view is refreshed?
body: <p>I'm trying to build an app that searches GitHub repositories. And I'm using MVVM and Moya. When I call a method to search I get table view reloaded before a server responds.</p>
  
  <p>So the app flow should be: 
  A user types in a search query into the search bar. In <code>searchBarSearchButtonClicked(_:)</code> of <code>SearchViewController</code> called my method to search. The method is inside <code>SearchViewModel</code>. So view model's method triggers <code>RepositoryService</code> and then <code>NetworkService</code>.</p>
  
  <p>I put some print statements to see an order of execution. And in the console, I got: <code>2 3 4 (Here the table view is refreshing) 1</code>. I've tried to use GCD in different places, also I tried to use barriers. At the end of the day, the table view is still refreshing before <code>print(1)</code> is called.</p>
  
  <p><strong>SearchViewController:</strong></p>
  
  <pre><code>extension SearchViewController: UISearchBarDelegate {
      func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
          guard let query = searchBar.text, query.count &gt; 2 else { return }
          viewModel.searchRepositories(withQuery: query) { [weak self] in
              guard let self = self else { return }
              print(4)
              print("Reloading...")
              self.tableView.reloadData()
          }
      }
  }
  </code></pre>
  
  <p><strong>SearchViewModel:</strong></p>
  
  <pre><code>var repositories = [Repository]()
  
  func searchRepositories(withQuery query: String, completion: @escaping () -&gt; Void) {
      repositories = repositoryService.searchRepositories(withQuery: query)
      print(3)
      completion()
  }
  </code></pre>
  
  <p><strong>RepositoryService:</strong></p>
  
  <pre><code>private var repositories = [Repository]()
  
  func searchRepositories(withQuery query: String) -&gt; [Repository] {
      networkService?.searchRepositories(withQuery: query) { [weak self] repositories in
          guard let self = self, let repositories = repositories else { return }
          self.repositories += repositories
      }
      print(2)
      return self.repositories
  }
  </code></pre>
  
  <p><strong>NetworkService:</strong></p>
  
  <pre><code>func searchRepositories(withQuery query: String,
                          completionHandler: @escaping (([Repository]?) -&gt; Void)) {
      provider?.request(.searchRepo(query: query)) { result in
          switch result {
          case .success(let response):
              do {
                  let repositories = try response.map(SearchResults&lt;Repository&gt;.self)
                  print(1)
                  completionHandler(repositories.items)
              } catch let error {
                  print(error.localizedDescription)
              }
  
          case .failure(let error):
              print(error)
          }
      }
  }
  </code></pre>