I've been struggling for a day to get this working correct. I have NSMutableArray of Items. Sometimes during the application lifecycle, the App will push and pop objects in there. I can usually do this on the AngularJS side with $scope.$watch. I've been using ReactiveCocoa in the meantime to do some of this behavior
Essentially I'd like a block to fire on these events.
-(void)fetchAllItems; -(void)push:(id)item; -(void)pop:(NSUInteger)index;
I tried this with ReactiveCocoa but it never fires!
[RACObserve(singletonStore, items) subscribeNext:^(NSArray *wholeArray) {
NSUInteger count = [wholeArray count];
[self.offerCircleButton setTitle:[NSString stringWithFormat:@"%d", count]]
}];
The items property is declared in the singletonStore object as
@property (nonatomic, copy) NSArray *items;
The getter and setter is for an NSMutableArray as a private variable
@interface SHSingletonStore()
{
NSMutableArray *_items;
}
@end
-(NSArray *)items {
return [_items copy];
}
-(void)setItems:(NSArray *)items{
if([_items isEqualToArray:items] == NO){
_items = [items mutableCopy];
}
}
Essentially I'd like a block to fire on these events.
-(void)fetchAllItems; -(void)push:(id)item; -(void)pop:(NSUInteger)index;
If what you are saying is that you want a signal that sends a value whenever any of those methods are called, you can do that with -rac_signalForSelector:
, like so:
SEL fetchAllSEL = @selector(fetchAllItems);
RACSignal *fetchAllSignal = [singletonStore rac_signalForSelector:fetchAllSEL];
SEL pushSEL = @selector(push:);
RACSignal *pushSignal = [singletonStore rac_signalForSelector:pushSEL];
SEL popSEL = @selector(pop:);
RACSignal *popSignal = [singletonStore rac_signalForSelector:popSEL];
@weakify(self);
[[RACSignal merge:@[ fetchAllSignal, pushSignal, popSignal ]] subscribeNext:^(id _) {
@strongify(self);
[self.offerCircleButton setTitle:[NSString stringWithFormat:@"%d", singletonStore.items.count]]
}];
This solution is kind of gross, because each of the individual merged signals sends a different type of value (so you end up with a signal that can send 3 different types of value, which is usually not cool), but if you are just ignoring the value so you can perform some side effect based on some global state, it doesn't really matter.