【问题标题】:The command is disabled and cannot be executed该命令被禁用,无法执行
【发布时间】:2017-08-01 12:08:24
【问题描述】:

所以,当我尝试获取一些数据时,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:^{
        }];
    }];
}

【问题讨论】:

    标签: objective-c reactive-cocoa racsignal raccommand


    【解决方案1】:

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

    但您可以测试该错误 - 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];
     }];
    

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 2016-08-20
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多