Search code examples
iosreactive-cocoa

Is it right that ReactiveCocoa's RACObserve is to replace the set methods for properities?


After create a instance of a class. I bind some properties by RAC macros.

_theTable = [[ScrollableTable alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[_theTable setScrollEnabled:YES];
[_theTable setBounces:YES];
[_theTable setBackgroundColor:[UIColor whiteColor]];
[_theTable setShowsVerticalScrollIndicator:YES];
[_theTable setShowsHorizontalScrollIndicator:YES];
[self.view addSubview:_theTable];

RAC(self.theTable, dataVO) = RACObserve(self.tableVM, tableDataVO);
RAC(self.theTable, styleVO) = RACObserve(self.tableVM, tableStyleVO);

Inside the ScrollableTable, I tried to use RACObserve macro to listen some event when self.dataVO and self.styleVO has been changed. When the first time the observes emit, the VOs are empty. So I'm wondering is that correct way to use ReactiveCocoa ?

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
    _isSeperateFill = YES;
    _isBorderStroke = NO;
    _isSeperatedStroke = YES;

    _contentWidth = 0;

    @weakify(self);
    [RACObserve(self, dataVO) subscribeNext:^(TableDataVO* dataVO){
        if( dataVO ){
            NSString* indexKey = [[dataVO.tableDataDictionary allKeys] objectAtIndex:0];
            @strongify(self);
            _keys = [self.dataVO.tableDataDictionary allKeys];
            _rows = [[self.dataVO.tableDataDictionary objectForKey:indexKey] count];

        }
    }];


    [RACObserve(self, styleVO) subscribeNext:^(TableStyleVO* styleVO){
        if( styleVO ){
            @strongify(self);
            self.styleVO.tableHeaderLineHorizontalMargin = styleVO.tableWidth / [_keys count] / 2;
        }
    }];

}
return self;
}

Solution

  • Yes, this is correct. When they are first observed they will not have their value set. You should then get a subsequence event emitted on the signal with the initial value.

    If you don't want the first value, just skip it!

    RACSignal *skipped = [RACObserve(self.tableVM, tableDataVO) skip:1];