动态OneToMany ReactiveCocoa绑定

时间:2017-05-27 16:41:53

标签: objective-c reactive-cocoa reactive-cocoa-3

我的代码中有类似于以下结构:

// Model
@interface Child : NSObject
@property (nonatomic, assign) CGPoint position;
@end

@interface Father : NSObject
@property (nonatomic, strong) NSArray <Child *> *children; // (0..n)
@end

// ViewModel
@interface FatherViewModel : NSObject
@property (nonatomic, strong) Father *father;
@property (nonatomic, assign) CGPoint averagePositionOfChildren;
@end

在执行期间,每个父成员中的子女数量可以更改(在这种情况下,我重新创建整个NSArray)并且每个孩子的位置也可以更改

ReactiveCocoa中是否存在任何优雅的解决方案,以便将模型中子项位置的动态数量映射到FatherViewModel中的averagePositionOfChildren?

2 个答案:

答案 0 :(得分:0)

是的,我可以看到你选择两种策略:

<强> 1。使用属性

MutableProperty使用children,然后为averagePositionOfChildren创建一个映射属性。

<强> 2。使用KVO

或者,您可以使用KVO观看children中的更改。即,您可以创建DynamicProperty

请注意,正如您已经指出的那样,这两种方案都会强制您重新创建整个数组。

答案 1 :(得分:0)

如果有人有兴趣,请遵循我自己的解决方案:

@interface FatherViewModel()
@property (nonatomic, strong) RACDisposable *averageBindingDisposable;
@end

@implementation FatherViewModel
- (instanceType) init {
self = [super init];
...
...
RACSignal *signalOfSignals = [RACObserve(self, father.children)
                         map:^RACSignal *(NSArray <Child *> *setOfChildren) {
                             NSArray <RACSignal *> *arrayOfSignal = [[[setOfChildren rac_sequence]
                                                                      map:^RACSignal *(Child *child) {
                                                                          return RACObserve(child, position);
                                                                      }]
                                                                     array];
                             return [RACSignal combineLatest:arrayOfSignal];
                         }];

[self.averageBindingDisposable dispose];
self.averageBindingDisposable = [[[signalOfSignals flatten]
                  map:^NSValue *(RACTuple *tuple) {
                      NSArray <NSValue *> *arrayOfWrappedCGPoints = tuple.allObjects;
                      CGPoint avg = CGPointZero;
                      // Compute here the average of the points in the array
                      return [NSValue valueWithPoint: avg];
                  }]
                  setKeyPath:@"averagePositionOfChildren" onObject: self];
}];

return self;
}
...
@end