Xaree Lee
Xaree Lee

Reputation: 3387

Combine values of RACSignal(s) but only triggered when the specific signal sends the next value (ReactiveCocoa)

I combine three signals together to get the latest values, but I only want the subscription is only triggered when the value of the specific signal (toppestStretchPercentageSignal) is changed.

How to deal with it?

The subscription of the following code will be triggered when any value of those three signals is changed.

[[RACSignal
    combineLatest:@[contentOffsetDidChangeSignal,
                    toppestVisibleItemSignal,
                    toppestStretchPercentageSignal]]
    subscribeNext:^(RACTuple *tuple) {
        NSLog(@"need to be only triggered when toppestStretchPercentageSignal sends the next value");
    }];

Update:

Thank jhosteny's answer. It looks like I just need to add sample: method after combineLatest:, doesn't it?

[[[RACSignal
    combineLatest:@[contentOffsetDidChangeSignal,
                   toppestVisibleItemSignal,
                   toppestStretchPercentageSignal]]
    sample:toppestStretchPercentageSignal]
    subscribeNext:^(id x) {
        NSLog(@"update toppest item frame");
    }];

Upvotes: 1

Views: 1087

Answers (1)

jhosteny
jhosteny

Reputation: 554

I think you want something like this:

[[RACSignal
     zip:@[toppestStretchPercentageSignal,
           [[RACSignal
               combineLatest:@[contentOffsetDidChangeSignal,
                               toppestVisibleItemSignal]]
            sample:toppestStretchPercentageSignal]]] subscribeNext:^(RACTuple *tuple) {
                RACTupleUnpack(NSNumber *toppestStretch, RACTuple *tuple2) = tuple;
                RACTupleUnpack(id contentOffset, id visibleItem) = tuple2;
            }];

See this comment in the ReactiveCocoa issues section on GitHub.

Upvotes: 1

Related Questions