-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Remove timer and listeners from Inspector window when it is closed #4546
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
Conversation
The Apple documentation for scheduledTimer says:
As the The Apple documentation for addObserver says:
Are we sure the code to remove the observers is required? I'm thinking it is only the If we do want to retain the code to remove the observers, then can't we simplify it and always use |
I do think this is correct. This PR more than just resource cleanup. It's also about improving efficiency by cutting down on wasted work. The The listeners are not as bad, since they will only do work when
I have never gotten a firm grasp on how observers are supposed to work. I suspect there are many variations. And I seem to be really terrible at finding documentation on this stuff. Thanks for the note on ...However, I tried replacing the loop in private func removeTimerAndListeners() {
updateTimer?.invalidate()
updateTimer = nil
NotificationCenter.default.removeObserver(self)
} Very strange behavior ensued. Somehow, the new call prevented the So... as far as I can tell, the |
45e2615
to
964de73
Compare
That certainly is not expected behavior. Thank you AppKit! We can still simplify this and remove the need for the NotificationCenter.default.removeObserver(self, name: .iinaFileLoaded, object: nil)
NotificationCenter.default.removeObserver(self, name: .iinaMainWindowChanged, object: nil) That should avoid whatever this interference with If there were a lot of observers I'd be inclined to go with the array. With only two, I'm kind of preferring this way. What do you think? |
I tried this: NotificationCenter.default.removeObserver(self, name: .iinaFileLoaded, object: nil)
NotificationCenter.default.removeObserver(self, name: .iinaMainWindowChanged, object: nil) They did not interfere with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pulled PR, built IINA, confirmed observers and timer properly removed. Looks good to me.
Looks to me like Apple's documentation is buggy as the doc for addObserver(forName:object:queue:using:) says:
Seems like a cut-n-paste error as apparently removeObserver(_:name:object:) can't be used if the observer was added with that particular add method. Closed source APIs like this are irritating. No way to tell if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description:
When the Inspector window is opened, it registers 2 observers and starts a recurring timer which pulls fresh data & updates its UI every 1s, and continues to do so even after closed, which wastes resources. This PR adds code which:
NSWindowDelegate
so thatwindowWillClose()
will be called on close. On window close, deregisters observers and shuts down the timer.