Last active
October 19, 2015 12:31
-
-
Save clmntcrl/248dd849a0d903bb4f5f to your computer and use it in GitHub Desktop.
Decouple UI from Model with View Models and Controls question. http://christiantietze.de/posts/2015/10/view-model-control/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetworkController { | |
// To simplify getSomeDataFromNetwork() send request synchronously | |
// and return a value on success, .None on failure. | |
func getSomeDataFromNetwork() -> AnyObject? {} | |
} | |
struct ViewModel { | |
var networkController: NetworkController | |
var error = false | |
var data = "A default value" | |
init() { | |
networkController = NetworkController() | |
refreshServerState() | |
} | |
mutating func refreshServerState() { | |
if let newData = networkController.getSomeDataFromNetwork() { | |
data = String(newData) | |
} else { | |
error = true | |
} | |
} | |
} | |
// My problem with having a copy of ViewModel in Presenter and ViewController: | |
// | |
// Your Presenter need to refresh server state every X minutes to update its views and your ViewController | |
// need to know if the request encounter an error to push an error view controller But each one has a | |
// different copy of ViewModel :( so they have to perform their own request. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment