Could someone provide a one-line example of using ReactiveCocoa abstractions to achieve something like this:
// pseudo-code
NSMutableArray *array = @[[] mutableC
You can't observe an array for changes. ReactiveCocoa uses key-value observation. Which, as the name suggests, only observes changes to keyed attributes (dictionary members, properties, etc.).
What you can do is observe an array property for changes:
@interface Blah : NSObject
@property (copy, readonly) NSArray *arrayProperty;
@end
// later...
Blah *blah = [Blah new];
[RACObserve(blah, arrayProperty) subscribeNext:^(NSArray *wholeArray){}];
If you want to know which objects where inserted/removed then you have two options. You could work it out by storing each array and comparing each with the previous. This is simplest but will perform badly with very large arrays. AFAIK, ReactiveCocoa does not have built-in operations to do this.
Or you could implement KVO collection accessors and ensure that changes to the array are made using mutableArrayValueForKey:
. This avoids creating a new array whenever any changes are made, and also notifies observers of changes made to the proxy array returned by mutableArrayValueForKey:
.
Observing change info with ReactiveCocoa is slightly more involved:
RACSignal *changeSignal = [blah rac_valuesAndChangesForKeyPath:@keypath(blah, arrayProperty) options: NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld observer:nil];
[changeSignal subscribeNext:^(RACTuple *x){
NSArray *wholeArray = x.first;
NSDictionary *changeDictionary = x.second;
}];
The change dictionary tells you what kind of change was made to the array, which objects were inserted/removed, and the indexes of the inserted/removed objects.
It is documented at: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html