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

Controller cannot release after observing a notification. #32

Closed
starlight173 opened this issue Nov 25, 2021 · 1 comment
Closed

Controller cannot release after observing a notification. #32

starlight173 opened this issue Nov 25, 2021 · 1 comment

Comments

@starlight173
Copy link

Hello, I got an issue that the view controller cannot release when observing a notification like this.

let observable = NotificationCenter.default.observable(for: Notification.Name.UIApplicationDidEnterBackground)
observe(observable) { notification in
    print("Application did enter background")
}

How to fix it ? Thanks.

@MaximeLM
Copy link

Hello, you should use [weak self] in your event handlers to avoid creating a retain cycle, like this:

let observable = NotificationCenter.default.observable(for: Notification.Name.UIApplicationDidEnterBackground)
observe(observable) { [weak self] notification in
    print("Application did enter background")
    self?.doSomething()
}

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