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

How to avoid unsubscribe after catch error or resubscribe #1218

Closed
vincentsong opened this issue Apr 2, 2014 · 6 comments
Closed

How to avoid unsubscribe after catch error or resubscribe #1218

vincentsong opened this issue Apr 2, 2014 · 6 comments
Labels

Comments

@vincentsong
Copy link

Hi Guys,

I have a some code like below

RAC(self, A) = [RACObserve(self, B) flattenMap:^id(id param) {
    return [self doSomthingToGetA:param];// this may throw an error
}] catch:^RACSignal *(NSError *error){

    //do some other things
    return [RACSignal return:nil];
}

when the error happened, next time I cannot get the signal for B anymore. So my question is there any way to avoid unsubscribe after catch error? Or do I need to use any other methods such as then: or something else?

@erikprice
Copy link

You can put -repeat at the end of your signal chain if you want to automatically have the signal be resubscribed-to if it completes. You can put -retry at the end of your signal chain if you want to automatically have the signal be resubscribed-to if it errors.

@vincentsong
Copy link
Author

Hi Erik,

If I put repeat at the end, the block in flattenMap will be called after error happened and then it falls to a infinite loop.

@erikprice
Copy link

What signal do you want A to be subscribed to if an error occurs?

@mdiep
Copy link
Contributor

mdiep commented Apr 2, 2014

If you put the -catch: inside the -flattenMap:, then the outer signal won't error.

RAC(self, A) = [RACObserve(self, B)
    flattenMap:^(id param) {
        return [[self
            doSomthingToGetA:param]; // this may throw an error
            catch:^(NSError *error){
                //do some other things
                return [RACSignal return:nil];
            }];
    }];

@vincentsong
Copy link
Author

Thanks mdlep, that fixed my problem.

@ChaosLin
Copy link

Excellent solution!Thank mdiep :)@mdiep

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

No branches or pull requests

5 participants