【问题标题】:Refactoring ReactiveCocoa重构 ReactiveCocoa
【发布时间】:2013-06-11 13:30:00
【问题描述】:

我在视图模型中有以下代码:

@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;
}];

它基本上是将属性绑定到私有属性上。这是完全错误的处理方式,还是有更简洁的方式来编写上述代码?

【问题讨论】:

    标签: ios mvvm viewmodel reactive-cocoa


    【解决方案1】:

    试试这个:

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

    来自RAC宏的头部注释:

    // 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;
    

    【讨论】:

    • 似乎不起作用。不确定它是否与调用代码的范围有关。 RAC() 和 RACAble() 究竟是如何工作的?他们是否在内部某处保留对被引用对象的引用?
    • 你应该把这段代码放在视图控制器的初始化序列中。 viewDidLoad 通常是一个很好的开始,因为关系在控制器的整个生命周期中都保持不变。不必在每次出现时都重新创建它们。
    • 这段代码在viewmodel类的init方法中调用的方法中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 2014-11-18
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多