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

Keep only necessary webviews alive in the app #2749

Merged
merged 3 commits into from
May 6, 2024
Merged

Conversation

bgoncal
Copy link
Member

@bgoncal bgoncal commented Apr 29, 2024

Summary

I noticed we are losing web views during the usage of the App, for example, when the user logs out we create a new web view after it logs in, but the old webview was kept in memory due to strong reference of WKScriptMessageHandler or due to not be listed in our cached webviews dictionary (and then not removed).

This PR gains controls of creation and usage of webviews aswell as removing them when not needed anymore, it also fixes the issue that some users experience "failure to authenticate" notifications (that were partially caused by these ghost webviews)

Screenshots

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#

Any other notes

@bgoncal
Copy link
Member Author

bgoncal commented Apr 29, 2024

I still have to test on macOS, for iOS it is working great now, no webview lost in the way

@bgoncal bgoncal marked this pull request as ready for review April 30, 2024 10:24
@bgoncal
Copy link
Member Author

bgoncal commented Apr 30, 2024

I still have to test on macOS, for iOS it is working great now, no webview lost in the way

Test done, in mac the scenario was even worse, everytime a window was opened then closed, that webview lived in memory until the app is killed

Sources/App/WebView/IncomingURLHandler.swift Outdated Show resolved Hide resolved
@@ -75,7 +75,7 @@ class WebViewWindowController {
// not changing anything, but handle the promises
updateRootViewController(to: rootController)
} else {
if let webViewController = WebViewController(restoring: .init(restorationActivity)) {
if let webViewController = makeWebViewIfNotInCache(restorationType: .init(restorationActivity)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could this code path hit a case where it's cached already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of the makeWebViewIfNotInCache is to make sure all webviews will be tracked in the cached dictionary, so in this case if it is not cached already it will then make a new webview and cache, otherwise we don't have how to control webviews that are created and left behind.

@bgoncal bgoncal merged commit 9a83285 into master May 6, 2024
5 checks passed
@bgoncal bgoncal deleted the dealloc-webviews branch May 6, 2024 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants