0

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?

Community
  • 1
  • 1
chasew
  • 8,438
  • 7
  • 41
  • 48
  • I think disposing the subscription is the cleanest way. Take a look at https://github.com/ReactiveCocoa/ReactiveCocoa/issues/468 – Akito Jul 26 '14 at 14:07
  • how about some example code using my specific situation? his is different – chasew Sep 15 '14 at 17:29

0 Answers0