这段代码是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()
}
与工作中的一些高级开发人员交谈后,他们告诉我这是他们通常的做法。由于将数组保留在视图模型内不会破坏MVVM模式,因此可以非常轻松地重用数据并清除保留其他字段的数据服务(使其更具可重用性)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句