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

Retain Cycle CocoaAction #235

Open
Marcin951 opened this issue Jul 12, 2021 · 2 comments
Open

Retain Cycle CocoaAction #235

Marcin951 opened this issue Jul 12, 2021 · 2 comments

Comments

@Marcin951
Copy link

Marcin951 commented Jul 12, 2021

Hey I noticed retain cycle in this example code

    let forgotPasswordAction = CocoaAction {
        return .create { [weak self] (observer) -> Disposable in
            guard let self = self else { return Disposables.create() }
            self.delegate?.didTapForgotPasswordButton()
            
            observer.onCompleted()
            return Disposables.create()
        }
    }
    
    forgotPasswordButton.rx.action = forgotPasswordAction

RxSwift 6.0.0
Action latest version from master

@Marcin951
Copy link
Author

I fixed it by moving weak self before return .create

    let forgotPasswordAction = CocoaAction { [weak self] in
        return .create { (observer) -> Disposable in
            guard let self = self else { return Disposables.create() }
            self.delegate?.didTapForgotPasswordButton()
            
            observer.onCompleted()
            return Disposables.create()
        }
    }

Now it works, but maybe that previous version of my code also should work?

@nflahavan
Copy link
Contributor

In the first code snippet you posted you are implicitly capturing self via a strong reference in the WorkFactory you're declaring. You are then explicitly capturing self via a weak reference in the subscribe function you're passing to the create function that you're using to create an observable.

If self has a strong reference to the CocoaAction and the CocoaAction has a strong reference to the workFactory (and therefore self) then you have your retain cycle. There isn't any change to Action that would mitigate this (CocoaAction must have a strong reference to the workFactory to keep it in memory).

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