0

因此,当我尝试获取一些数据时,RACCommand 会返回此错误。

例如,我有一个选择器,当用户滚动它时,应用程序从服务器获取数据并显示它们,但如果用户快速滚动,(上一个操作正在进行中)RACCommand 会收到此错误:

Error Domain=RACCommandErrorDomain Code=1 "The command is disabled and cannot be executed" UserInfo={RACUnderlyingCommandErrorKey=<RACCommand: 0x174280050>, NSLocalizedDescription=The command is disabled and cannot be executed}

我知道,它与一些取消机制有关,但是我尝试了很多示例并且效果不佳。

它是我的一段代码:

    @weakify(self);
    [[[self.viewModel makeCommand] execute:nil]
     subscribeError:^(NSError *error) {
         @strongify(self);
         [self showAlertWithError:error];
     }];

和视图模型:

- (RACCommand*)makeCommand {
    if (!_makeCommand) {
        _makeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            return [self getVehicleMake];
        }];
    }
    return _makeCommand;
}

- (RACSignal*)getVehicleMake {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[self.forumService getForumMakesWithYear:@([self.selectedYear integerValue])
                                         category:self.vehicleCategory]
         subscribeNext:^(RACTuple *result) {
             self.makes = result.first;
             [subscriber sendNext:self.makes];
         } error:^(NSError *error) {
             [subscriber sendError:error];
         } completed:^{
             [subscriber sendCompleted];
         }];

        return [RACDisposable disposableWithBlock:^{
        }];
    }];
}
4

1 回答 1

0

RACCommand默认情况下不允许并发执行。当它执行时,它会被禁用。如果您尝试再次执行,它将发送该错误。

但是您可以测试该错误——<code>RACCommand 具有可用RACCommandErrorDomainRACCommandErrorNotEnabled常量。

@weakify(self);
[[[self.viewModel makeCommand] execute:nil]
 subscribeError:^(NSError *error) {
     @strongify(self);
     if ([error.domain isEqual:RACCommandErrorDomain] && error.code == RACCommandErrorNotEnabled) {
         return;
     } 
     [self showAlertWithError:error];
 }];
于 2017-08-02T12:54:25.040 回答