为了解决这个问题,我已经努力了一天。我有NSMutableArray的项目。有时,在应用程序生命周期中,应用程序会在其中推送和弹出对象。我通常可以使用$ scope。$ watch在AngularJS上执行此操作。在此期间,我一直在使用ReactiveCocoa
本质上,我希望在这些事件上触发一个障碍。
-(void)fetchAllItems; -(void)push:(id)item; -(void)pop:(NSUInteger)index;
我用ReactiveCocoa尝试过,但是它永远不会触发!
[RACObserve(singletonStore, items) subscribeNext:^(NSArray *wholeArray) {
NSUInteger count = [wholeArray count];
[self.offerCircleButton setTitle:[NSString stringWithFormat:@"%d", count]]
}];
items属性在singletonStore对象中声明为
@property (nonatomic, copy) NSArray *items;
getter和setter用于NSMutableArray作为私有变量
@interface SHSingletonStore()
{
NSMutableArray *_items;
}
@end
-(NSArray *)items {
return [_items copy];
}
-(void)setItems:(NSArray *)items{
if([_items isEqualToArray:items] == NO){
_items = [items mutableCopy];
}
}
本质上,我希望在这些事件上触发一个障碍。
-(void)fetchAllItems; -(void)push:(id)item; -(void)pop:(NSUInteger)index;
如果您要说的是要在每次调用这些方法中的任何一个时发出一个发送值的信号,则可以使用进行操作-rac_signalForSelector:
,如下所示:
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]]
}];
这种解决方案是总的,因为每个单独的合并信号都发送不同类型的值(因此您最终得到的信号可以发送3种不同类型的值,通常这并不酷),但是如果您忽略了该值,以便您可以基于某些全局状态执行某些副作用,这并不重要。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句