Cancel RACCommand execution

a 夏天 提交于 2019-12-11 10:58:10

问题


Is there any way to cancel execution of a RACCommand?

For example I have a command with infinite execution signal like this:

RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        __block BOOL stop = NO;

        while (!stop) {
            [subscriber sendNext:nil];
        }

        return [RACDisposable disposableWithBlock:^{
            stop = YES;
        }];
    }];
}];

So how can I stop it after calling [command execute:nil]?


回答1:


I'm a bit new to RACCommand, so I'm not certain there's a better way to do this. But I have been using takeUntil: with a cancellation signal to halt execution.

RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        while (true) {
            [subscriber sendNext:nil];
        }
    }] takeUntil:cancellationSignal];
}];


来源:https://stackoverflow.com/questions/23784097/cancel-raccommand-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!