简体   繁体   中英

The command is disabled and cannot be executed

So, when I trying to fetch some data, RACCommand return this error.

I have a picker for example and when user scroll it, app get data from server and show them, but if user scroll fast, (previous operation in progress) RACCommand get this error:

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}

I know, its related with some cancel mechanism, but I tried many examples and not working as well.

Its my piece of code:

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

and viewModel:

- (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:^{
        }];
    }];
}

RACCommand doesn't allow concurrent execution by default. When it's executing, it becomes disabled. If you try to execute again, it will send that error.

But you can test for that error— RACCommand has RACCommandErrorDomain and RACCommandErrorNotEnabled constants available.

@weakify(self);
[[[self.viewModel makeCommand] execute:nil]
 subscribeError:^(NSError *error) {
     @strongify(self);
     if ([error.domain isEqual:RACCommandErrorDomain] && error.code == RACCommandErrorNotEnabled) {
         return;
     } 
     [self showAlertWithError:error];
 }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM