Ios 在捕捉到错误后,按钮信号不会再次触发

Ios 在捕捉到错误后,按钮信号不会再次触发,ios,objective-c,iphone,reactive-cocoa,Ios,Objective C,Iphone,Reactive Cocoa,如果我将UIControlEventTouchUpInside信号添加到doneButton,并调用API,如果API失败,将调用捕获。但如果我再次尝试单击按钮,则不会触发按钮控件事件 - (void)viewDidLoad { [super viewDidLoad]; [[[[[self.doneButton rac_signalForControlEvents:UIControlEventTouchUpInside] doNext:^(id x) { [SV

如果我将
UIControlEventTouchUpInside
信号添加到
doneButton
,并调用API,如果API失败,将调用捕获。但如果我再次尝试单击按钮,则不会触发按钮控件事件

- (void)viewDidLoad {
    [super viewDidLoad];

    [[[[[self.doneButton rac_signalForControlEvents:UIControlEventTouchUpInside] doNext:^(id x) {
        [SVProgressHUD show];
    }] flattenMap:^RACStream *(id value) {
        return [[HttpService sharedService] updateImageData:UIImageJPEGRepresentation(self.signatureImageView.image, 0.5)];
    }] catch:^RACSignal *(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        return [RACSignal empty];
    }] subscribeNext:^(id x) {
        [SVProgressHUD dismiss];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
}

我想这条线会有帮助的

如果信号出现故障/错误,将自动取消订阅。您可以使用
-retry
,但这将继续尝试您的操作,直到is没有失败,如果存在永久性问题,它将无限期地循环

flattmap
中包装此条件将捕获问题,而无需取消订阅最初的
rac\u signalForControlEvents
观察

请在GitHub上的上面的线程中查看mdiep的注释,或者执行类似操作

[[[[self.doneButton rac_signalForControlEvents:UIControlEventTouchUpInside] doNext:^(id x) {
    [SVProgressHUD show];
}] flattenMap:^RACStream *(id value) {
    return [[[HttpService sharedService] updateImageData:UIImageJPEGRepresentation(self.signatureImageView.image, 0.5)] 
      catch:^RACSignal *(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        return [RACSignal empty];
      }];
}] subscribeNext:^(id x) {
    [SVProgressHUD dismiss];
    [self.navigationController popToRootViewControllerAnimated:YES];
}];

我还没有用这段代码构建一个测试。只需根据您的
HttpService
类中可能存在的内容进行猜测。

您可以使用
RACCommand
来解决此问题

RACCommand *doneCommand =
[[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSString *selected) {

    return  [[[self updateImageSignal]
              doCompleted:^{
                  [SVProgressHUD dismiss];
                  [self.navigationController popToRootViewControllerAnimated:YES];
              }] doError:^(NSError *error) {
                  [SVProgressHUD showErrorWithStatus:error.localizedDescription];
              }];
}];

self.doneButton.rac_command = doneCommand;
现在创建
RACSignal
,根据您的请求发送成功和错误

-(RACSignal *)updateImageSignal {
@weakify(self)
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    @strongify(self)
    [[HttpService sharedService] updateImageData:UIImageJPEGRepresentation(self.signatureImageView.image, 0.5)
                                        complete:^(BOOL success) {
                                            if(success)
                                                [subscriber sendNext:@(success)];
                                            else
                                                [subscriber sendError:nil];
                                            [subscriber sendCompleted];
                                        }];
    return nil;
}];
}
-(RACSignal*)更新图像信号{
@weakify(自我)
返回[RACSignal createSignal:^RACSignal*(id订户){
@强化(自我)
[[HttpService sharedService]updateImageData:UIImageJPEG表示(self.signatureImageView.image,0.5)
完成:^(BOOL成功){
如果(成功)
[用户发送下一步:@(成功)];
其他的
[订户发送错误:无];
[用户发送完成];
}];
返回零;
}];
}

希望它能帮助你。如果您有任何问题,请随时提问。

请检查我的答案。