I've been reading up on ReactiveCocoa v3 lately and I'm struggling with just setting up basic stuff. I've already read the changelog, the tests, the few SO questions and the articles by Colin Eberhardt on the subject. However, I'm still missing examples on basic bindings.
Let's say I have an app that presents the menu of the day. The app is using RAC3 and the MVVM pattern.
Model (Menu)
The model has one simple method for fetching todays menu. As for now, this don't do any network requests, it basically just creates a model object. The mainCourse
property is a String
.
class func fetchTodaysMenu() -> SignalProducer<Menu, NoError> {
return SignalProducer {
sink, dispoable in
let newMenu = Menu()
newMenu.mainCourse = "Some meat"
sendNext(sink, newMenu)
sendCompleted(sink)
}
}
ViewModel (MenuViewModel)
The view model exposes different String
variables for letting the view controller show the menu. Let's just add one property for showing the main course.
var mainCourse = MutableProperty("")
And we add a binding for this property:
self.mainCourse <~ Menu.fetchTodaysMenu()
|> map { menu in
return menu.mainCourse!
}
ViewController (MenuViewController)
Last but not least, I want to present this main course in a view. I'll add a UILabel
for this.
var headline = UILabel()
And finally I want to set the text
property of that UILabel by observing my view model. Something like:
self.headline.text <~ viewModel.headline.producer
Which unfortunately does not work.
Questions
- The method
fetchTodaysMenu()
returns aSignalProducer<Menu, NoError>
, but what if I want this method to return aSignalProducer<Menu, NSError>
instead? This would make my binding in my view model fail as the method now may return an error. How do I handle this? - As mentioned, the current binding in my view controller does not work. I've been playing around with creating a
MutableProperty
that represents thetext
property of theUILabel
, but I never got it right. I also think it feels clumsy or verbose to have to create extra variables for each property I want to bind. This was not needed in RAC2. I intentionally also tried to avoid usingDynamicProperty
, but maybe I shouldn't? I basically just want to find the right way of doingRAC(self.headline, text) = RACObserve(self.viewModel, mainCourse);
.
Any other feedback/guidance on how to make this basic setup is highly appreciated.