简体   繁体   中英

Reload TableView when API completes w/ MVVM

I am working on an app in Swift 4. I am building my app programmatically - no storyboards.

I have a ViewController that looks like this

class SearchController: UITableViewController {

    private let cellID = "cellID"

    fileprivate let searchController = UISearchController(searchResultsController: nil)
    fileprivate let viewModel = SearchViewModel(client: DiscogClient())

    override func viewDidLoad() {
        super.viewDidLoad()

        setupSearchBar()
        setupTableView()
    }

    fileprivate func setupSearchBar() -> Void {
        navigationItem.hidesSearchBarWhenScrolling = false
        navigationItem.searchController = searchController

        searchController.searchBar.delegate = self

    }

    fileprivate func setupTableView() -> Void {
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        tableView.tableFooterView = UIView()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.cellViewModels.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        return cell
    }

}

extension SearchController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        viewModel.search(term: searchText)
    }

}

And I have a ViewModel that looks like this

import Foundation
import UIKit

class SearchViewModel {
    var cellViewModels: [Result] = []

    private let client: APIClient
    var results: SearchResults? {
        didSet {
            for result in (results?.results)! {
                cellViewModels.append(result)
            }
        }
    }

    var isLoading: Bool = false {
        didSet {
            showLoading?()
        }
    }

    var showLoading: (() -> Void)?
    var reloadData: (() -> Void)?
    var showError: ((Error) -> Void)?

    init(client: APIClient) {
        self.client = client
    }

    func search(term: String) {

        if let client = client as? DiscogClient {
            let endpoint = DiscogsEndpoint.search(term: term, searchType: .release)
            client.fetch(with: endpoint) { (either) in

                switch either {
                case .success(let results):
                    self.results = results
                case .error(let error):
                    self.showError?(error)
                }

            }
        }
    }
}

When I check didSet on var results: SearchResults? I can see that the value is being set via my APIClient .

What I would to do is instruct my ViewController table to reload it's data once that value is set.

I am unsure how to achieve this though? I thought I could use a didSet on cellViewModels and reach out to a method on my ViewController. This does not work or I do not understand how to do this yet.

I am hoping to see for now, the number of rows be updated via viewModel.cellViewModels.count within my SearchController

You can modify your search to accept completion block

func search(term: String, @escaping completion :() -> ()) {

        if let client = client as? DiscogClient {
            let endpoint = DiscogsEndpoint.search(term: term, searchType: .release)
            client.fetch(with: endpoint) { (either) in

                switch either {
                case .success(let results):
                    self.results = results
                    completion()
                case .error(let error):
                    self.showError?(error)
                    completion()
                }

            }
        }
    }

Finally you can reload tableView

    self.search(term: "abcd") {
        self.tableView.reloadData()
    }

People might often ask you to implement protocols and establish communication between ViewModel and View. But whether to use blocks or protocols is always a design implementation details :)

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM