I understand the difference between a value type and a reference type in Swift, and I realize that value types are intended to be used unchanged. But structures, in particular, have the ability to mutate on their own, and this is my concern. How to effectively use these mutating data types so that they retain their state or changes.
My question is: in iOS app. Should an MVVM project consider a model as a class or structure? . The model / model list is used as the presentation model, and these model instances can change over time (for example, the view model receives more model instances from the web service request and adds the model array to it), as the view controller view model is updated with this change. Here is a pseudo-example, but first some considerations and brief ones:
- I do not use any bindings. (No RxSwift).
- No KVO, I use delegation or completion handler processing.
Example:
- The controller view has an instance of the view model. When it appears, it requests a view model to retrieve data from the web service.
- .
- View model .
- , .
:
struct DummyViewModel {
private var ints:[Int] = []
var count: Int {
return ints.count
}
init() {}
mutating func fetchData(completionHandler:(NSError? ) -> Void) {
Networking.getDataFromRemote() { response in
self.ints.append(1)
completionHandler(nil)
}
}
}
:
class DummyViewController: UIViewController{
private var dummyViewModel: DummyViewModel?
override func viewDidLoad() {
super.viewDidLoad()
dummyViewModel = DummyViewModel()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
loadData()
}
}
private extension DummyViewController {
func loadData() {
dummyViewModel?.fetchData { error in
if self.dummyViewModel?.count > 0 {
print("View model updated")
} else {
print("View model is still same")
}
}
}
}
, , , , , . , , ?
, , . , . .
? - , , . , , . , , , . .
?