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
Safe to dispose of a subscription this way? Better way? #963
Comments
Unfortunately no, this (probably) isn't safe:
- (void)viewDidAppear:(BOOL)animated {
RACDisposable *disposable = [Singleton.command.executing subscribeNext:^(id x) {
BOOL isExecuting = [x boolValue];
if (isExecuting) {
// The command is currently executing.
// Display progress indicator.
} else {
// The command has finished executing.
// Remove progress indicator (if shown) and do something with the command's data.
// Unsubscribe from the command's executing signal.
[serialDisposable dispose];
serialDisposable = nil;
}
}
serialDisposable.disposable = disposable;
} |
Ah, ok... so, to be sure I understand, I didn't know about the Thanks! |
|
Gotcha. Thanks!! |
@joshaber I think you meant |
I did indeed! That's important. Thanks @Coneko. |
If I'm understanding correctly, you could get the same effect with: [[[[Singleton.command.executing
ignore:@NO]
doNext:^(id _) {
// The command is currently executing.
// Display progress indicator.
}]
takeUntil:[Singleton.command.executing ignore:@YES]]
subscribeCompleted:^{
// The command has finished executing.
// Remove progress indicator (if shown) and do something with the command's data.
}]; This avoids any need to manage disposables by hand. |
Brilliant! This works perfectly in my scenario. I am kicking off the Singleton's At this point, the table view controller's The I guess I should have explained all that to begin with... Sorry! |
In one of my view controllers, I am subscribing to a RACCommand that lives in a singleton object, like this (code is simplified):
Basically, when this view controller is loaded and pushed onto my nav controller, the Singleton's command may or may not be executing. In fact, it is possible that this view controller may be popped, and a new instance pushed again on the nav controller stack, while the Singleton's command's original execution is still active in the background.
Since the RACCommand lives in a singleton, it is never dealloc'd, so it seems I have to unsubscribe manually from its executing signal.
I have tried takeUntilBlock:(... return !executing), however, this prevents my subscriber from being called when executing sends next -> NO; I cannot tell when the command finishes executing.
Is there another, more obvious/simpler, way to do this? Is it "safe" to unsubscribe the way I have shown here?
Also... thanks for ReactiveCocoa! It's awesome!
The text was updated successfully, but these errors were encountered: