JFoulkes
JFoulkes

Reputation: 2429

Refactoring ReactiveCocoa

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?

Upvotes: 0

Views: 217

Answers (1)

Sonny Saluja
Sonny Saluja

Reputation: 7287

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;

Upvotes: 2

Related Questions