I'm writing an app in VIPER architecture for the first time, and can't understand if the UITableView
delegate and datasource methods should go into the View, Presenter or the Interactor? I found in some links that it should be part of View class, but that doesn't seem to be right. And even if it's part of View, how will the data reach there because View should technically not ask for data from the presenter. Presenter should push data itself.
The link's you read were correct, the delegate and datasource methods for an UITableView
in an app with VIPER architecture should stay in the View
.
About your conclusion about how the data will reach the view, it's wrong because the View
itself should ask for Presenter
bring the data, and then the Presenter ask for Interactor
load the data from web or a database.
If you have any questions about the VIPER architecture I definitely recommend these articles:
Article 1: https://blog.mindorks.com/building-ios-app-with-viper-architecture-8109acc72227
Article 2: https://cheesecakelabs.com/blog/best-practices-viper-architecture/
Article 3: https://cheesecakelabs.com/blog/ios-project-architecture-using-viper/
Yes, data source and delegate are the parts of a view layer.
If you do not want your view to ask presenter for data, then you can do it like I describe it. The data source class holds viewModels(dummy objects). You can then communicate via an interface. I mean you might understand better on some example:
protocol SomeViewProtocol {
func set(withVMS vms: [SomeViewModel])
}
final class SomeVC: SomeViewProtocol {
let dataSource: SomeDataSource
let tableView: UITableView
override func viewDidLoad() {
tableView.dataSource = dataSource
}
func set(withVMS vms: [SomeViewModel]) {
someDataSource.set(withVMS: vms)
tableView.reloadData()
}
}
protocol SomePresenterProtocol {
...
}
final class SomePresenter: SomePresenterProtocol {
fileprivate let view: SomeViewProtocol
//After view did load
func initAfterLoad() {
.
.
.
view.set(withVMS: viewModels)
}
}
But from my perspective, there is nothing wrong with View asking the presenter for the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With