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

Assign to self causes retain cycle #3

Open
georgescumihai opened this issue Apr 8, 2020 · 4 comments
Open

Assign to self causes retain cycle #3

georgescumihai opened this issue Apr 8, 2020 · 4 comments

Comments

@georgescumihai
Copy link

Using assign with self causes a retain cycle.

For more details check this forum post or stackoverflow post

@georgescumihai georgescumihai changed the title Assign to self causes retain Assign to self causes retain cycle Apr 8, 2020
@bednar
Copy link

bednar commented May 15, 2020

Hi @georgescumihai,

did you find solutions?

Regards

@georgescumihai
Copy link
Author

@bednar You can see the solutions in the forum or stack overflow link.

@debisani
Copy link

debisani commented Jun 22, 2020

//.assign(to: \.state, on: self)
.sink{[weak self] (value) in self?.state = value}

@DanSkeel
Copy link

DanSkeel commented Nov 29, 2020

Look at this solution assignNoRetain
https://forums.swift.org/t/does-assign-to-produce-memory-leaks/29546/9

extension Publisher where Self.Failure == Never {
    public func assignNoRetain<Root>(to keyPath: ReferenceWritableKeyPath<Root, Self.Output>, on object: Root) -> AnyCancellable where Root: AnyObject {
        sink { [weak object] (value) in
            guard let object = object else { return }
            _ = Just(value).assign(to: keyPath, on: object)
        }
    }
}

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

4 participants