0

I have the following code in a viewmodel:

@weakify(self);
[RACAbleWithStart(self.visitStartDate) subscribeNext:^(NSDate *visitStartDate) {
    @strongify(self);
    self.visit.startDate = visitStartDate;
}];
[RACAbleWithStart(self.visitEndDate) subscribeNext:^(NSDate *visitEndDate) {
    @strongify(self);
    self.visit.endDate = visitEndDate;
}];
[RACAbleWithStart(self.visitFocus) subscribeNext:^(NSString *focus) {
    @strongify(self);
    self.visit.actionPlan.focus = focus;
}];
[RACAbleWithStart(self.allDayVisit) subscribeNext: ^(NSNumber *allDayVisit) {
    @strongify(self);
    self.visit.allDay = allDayVisit;
}];

It is basically binding properties onto a private property. Is this totally the wrong way of going about this or is there a tidier way of writing the above code?

JFoulkes
  • 2,429
  • 1
  • 21
  • 24

1 Answers1

2

Try this:

RAC(self.visit, startDate) = RACAbleWithStart(self.visitStartDate);

From the header comment of the RAC macro:

// Lets you assign a keypath / property to a signal. The value of the keypath or
// property is then kept up-to-date with the latest value from the signal.
//
// If given just one argument, it's assumed to be a keypath or property on self.
// If given two, the first argument is the object to which the keypath is
// relative and the second is the keypath.
//
// Examples:
//
//  RAC(self.blah) = someSignal;
//  RAC(otherObject, blah) = someSignal;
Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39
  • Doesn't seem to work. Not sure if it's to do with the scope of where the code is called. How exactly do RAC() and RACAble() work? Do they keep a reference internally somewhere to the referenced objects? – JFoulkes Jun 15 '13 at 16:25
  • 2
    You should put this code in the initialization sequence of the view controller. `viewDidLoad` is usually a good play to start with as the relations are kept throughout the lifetime of the controller. They do not have to be recreated on every appearance. – allprog Jun 15 '13 at 20:41
  • This code is in a method which is called in the init method of a viewmodel class – JFoulkes Jun 17 '13 at 12:56