【发布时间】:2017-01-15 16:01:36
【问题描述】:
这段代码是 MVVM 架构的正确实现吗?我想知道是否可以将下载的数组保存在私有属性中以供将来在 TableView 中使用,还是应该不惜一切代价避免这种情况?
代码:
import Foundation
class StopsViewModel {
weak var delegate: StopsViewModelDelegate?
private let dbService: DatabaseService
private var stops = [Stop]()
init(withDbService dbService: DatabaseService) {
self.dbService = dbService
}
func loadStops() {
dbService.getStops(completion: { [weak self] stops in
self?.stops = stops
self?.delegate?.getStopsCallFinished()
})
}
func getStop(atIndex index: Int) -> Stop {
return self.stops[index]
}
func getRowCount() -> Int {
return self.stops.count
}
func getSectionsCount() -> Int {
return 1
}
}
protocol StopsViewModelDelegate: class {
func getStopsCallFinished()
}
【问题讨论】: