Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RACCommand's executionSignals's subscribeNext block or error's block execute more times? #84

Closed
longjianjiang opened this issue Mar 31, 2017 · 4 comments

Comments

@longjianjiang
Copy link

i use RACCommand to send a network request in a sendController, you can click this controller right barbuttomitem, then will execute a command; if success, this sendController will be dismissed;
but when fail, you know i add the log in the errors's subscribeNext block; i click barbuttomitem first time, the log will be show one time; but when i click at the second time, the log will be show two times; and it will always added one times if i continue click;
so, i want to know is there some solution to settle this problem? thanks!

@mdiep
Copy link
Contributor

mdiep commented Mar 31, 2017

You only need to call the error's subscribeNext: once. It sounds like you're calling it multiple times. Can you share some code?

@longjianjiang
Copy link
Author

sure; the view like below picture;
simulator screen shot 2017 4 1 09 30 09

if the server send error like 403(cause my app don't be verified so the server limit the interface call times). And the user can did this sendBtn more times, so the errors subscribeBlock can be executed more times, as same as the command be executed more times too; Here is my code, so can i have some solution to avoid this kind of problem?

[[_sendBtn rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(id x) {
            
            [weakSelf.sendViewModel.sendCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"已发送";
                [hud hide:YES afterDelay:1.0];

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.view endEditing:YES];
                    [self dismissViewControllerAnimated:YES completion:nil];
                });
            }];
            
            
            [weakSelf.sendViewModel.sendCommand.errors subscribeNext:^(id x) {
                LJLog(@"()()()()"); // click more times the text will be loged more times
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"新浪服务器开小差了...";
                [hud hide:YES afterDelay:1.0];
            }];
            
            NSDictionary *dict = @{@"statusd":_textView.text};
            [self.sendViewModel.sendCommand execute:dict];
        }];

@mdiep
Copy link
Contributor

mdiep commented Apr 1, 2017

You shouldn't subscribe to errors or executionSignals every time the button is pressed. It should only happen once.

            [weakSelf.sendViewModel.sendCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"已发送";
                [hud hide:YES afterDelay:1.0];

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.view endEditing:YES];
                    [self dismissViewControllerAnimated:YES completion:nil];
                });
            }];
            
            
            [weakSelf.sendViewModel.sendCommand.errors subscribeNext:^(id x) {
                LJLog(@"()()()()"); // click more times the text will be loged more times
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"新浪服务器开小差了...";
                [hud hide:YES afterDelay:1.0];
            }];

[[_sendBtn rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(id x) {
            NSDictionary *dict = @{@"statusd":_textView.text};
            [self.sendViewModel.sendCommand execute:dict];
        }];

@longjianjiang
Copy link
Author

ok, thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants