Search code examples
ioskey-value-observingreactive-cocoakey-value-codingkvc

Reactive Cocoa diferences between KVC and instance object


I use this code to subscribe events with my mutable array:

[RACObserve(self, marray) subscribeNext:^....

I am new in RAC and I don't understand why for example if I use:

NSMutableArray *keyValueCodingArray = [self mutableArrayValueForKey:@"marray"];

[keyValueCodingArray addObject:@"string"];

everything works ok and if I add something to my keyValueCodingArray which is my marray it reacts on events and if I remove some object from keyValueCodingArray it also react.

But if use the instance directly the RACObserve block wont work. I mean seems like it does not observe any changes with array:

[self.marray removeObjectAtIndex:0];

What is the differences?


Solution

  • What you are probably looking for is the following:

    [[keyValueCodingArray rac_signalForSelector:@selector(addObject:)] subscribeNext:^(id x) {
        <#code#>
    }];
    

    and

    [[keyValueCodingArray rac_signalForSelector:@selector(removeObject:)] subscribeNext:^(id x) {
        <#code#>
    }];
    

    Your code only checks the changes on the array property, not calls to it's value.