I have a semi-working approach to this problem. I have a search field that sets off a series of dependent cold RACSignals - in my case network requests (similar to Chaining dependent signals in ReactiveCocoa). If at any point during those network requests, the text changes again, I would like the current chain of signals to halt, and restart with the new search text. I am aware of functions like switchToLatest
, and this thread: https://github.com/ReactiveCocoa/ReactiveCocoa/issues/468, but not how they apply to my situation. Here is an example in code of what I am trying to achieve:
RACSignal * (^longNetworkSignalWithValue)(NSString *) = ^(NSString * value){
return [RACSignal startEagerlyWithScheduler:[RACScheduler scheduler] block:^(id<RACSubscriber> subscriber) {
usleep(2000000);
[subscriber sendNext:value];
}];
};
[[self.searchField.rac_textSignal throttle:.2] subscribeNext:^(id value) {
__block BOOL cancelled;
[[[self.searchField.rac_textSignal skip:1] take:1] subscribeNext:^(id x) {
NSLog(@"CANCELLED FOR : %@", value);
cancelled = YES;
}];
[[[longNetworkSignalWithValue(value) flattenMap:^RACStream *(id value) {
if(cancelled) return nil;
NSLog(@"Network signal 1 completed for: %@", value);
return longNetworkSignalWithValue(value);
}] flattenMap:^RACStream *(id value) {
if(cancelled) return nil;
NSLog(@"Network signal 2 completed for: %@", value);
return longNetworkSignalWithValue(value);
}] subscribeNext:^(id x) {
if(!cancelled)
NSLog(@"Completed with value: %@", x);
}];
}];
Is my cancelled BOOL the best way to accomplish what I am trying to do?