本文介绍了取消RACCommand执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法取消执行 RACCommand



例如,我有一个命令无限执行信号如下:

  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;
}];
}];
}];因此,如何在调用 [command execute:nil]之后停止它


$ b

解决方案

我对RACCommand有点陌生,所以我不确定有更好的方法来做到这一点。但我一直用 takeUntil:来取消信号。

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


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]?

解决方案

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];
}];

这篇关于取消RACCommand执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 16:58