1

I have a little confused about dispose mechanism of RACCommand. I want to dispose subscription of executing signal manually, but disposableBlock invokes only after sendCompleted event. It's a problem of my real task, hope someone can help me.

- (void)test {
    RACCommand *rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [self signal];
    }];

    RACDisposable *rac_dispose = [[rac_command execute:nil] subscribeNext:^(id x) {}];
    [rac_dispose dispose];
}

- (RACSignal *)signal {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
        });
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"dispose block");
        }];
    }];
}

1 Answer 1

0

The subscriptions to the signals that your block parameter (to the initializer) creates are private to the RACCommand, and it offers no API to dispose those subscriptions. The RACCommand will keep that subscription alive until it completes, regardless of whether your dispose of the signal returned from -[RACCommand execute:].

In other words, there is unfortunately no way to do what you are trying to do. If you need the ability to cancel the behavior of the signals that your block parameter creates, consider having those signals check for some condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.