Given the below code, how would you update a name label IBOutlet? class PreferencesVC: UIViewController { var profile: Profile! func updateViews() { // Update nameLbl } } class Profile { var name: String? func update(name newName: String) { name = newName } }
Sigiloso
He was looking for me to catch potential retain cycles and also make sure UI things were only happening on the main thread. We eventually got into a discussion on where the best place to put Dispatch.main. I think I preferred to go to the main thread as close to the actual UILabel objects as possible, while he preferred to split it up across data-model/view lines (i.e. the “self.onUpdate” line in the Profile object). In other words, prepare to defend your architecture decisions. class PreferencesVC: UIViewController { var profile: Profile! func updateViews() { Dispatch.main( self.nameLbl.text = profile.name ) } func viewDidLoad() { updateViews() self.profile.onUpdate = { [weak self] in self.updateViews() } } } class Profile { var name: String? var onUpdate : (() -> ())? func update(name newName: String) { let request = // ... request.fire(newName, completion: { validName in name = validName self.onUpdate?() } } }