Skip to content

Instantly share code, notes, and snippets.

@dmhts
Last active July 27, 2018 17:03
Show Gist options
  • Save dmhts/f4a52056b98a3b03d48095e807616746 to your computer and use it in GitHub Desktop.
Save dmhts/f4a52056b98a3b03d48095e807616746 to your computer and use it in GitHub Desktop.
// MARK: Delegate approach
import UIKit
protocol ExampleViewModelDelegate: class {
func someEventDidOccur()
}
class ExampleViewModel {
weak var delegate: ExampleViewModelDelegate?
func someHandler() {
delegate?.someEventDidOccur()
}
}
class ExampleViewController : UIViewController {
var viewModel = ExampleViewModel()
public override func viewDidLoad() {
super.viewDidLoad()
viewModel.delegate = self
}
}
extension ExampleViewController: ExampleViewModelDelegate {
func someEventDidOccur() {
// handle the event
}
}
// MARK: Reactive approach
import UIKit
import ReactiveCocoa
class ExampleViewModel {
var someEvent: SignalProducer<Void, NoError> = SignalProducer { sink, _ in
sink.send(value: ())
sink.sendCompleted()
}
}
class ExampleViewController : UIViewController {
var viewModel = ExampleViewModel()
public override func viewDidLoad() {
super.viewDidLoad()
viewModel.someEvent.startWithValues {
// handle the event
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment