Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix error call to remove observer
  • Loading branch information
bparrishMines committed Jun 14, 2022
commit 286a6f1c4eb1531187ab54c4100f900037533c8f
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class _WebKitWebViewWidgetState extends State<WebKitWebViewWidget> {
);
}

@override
void dispose() {
super.dispose();
controller._dispose();
}

@override
Widget build(BuildContext context) {
return widget.onBuildWidget(controller);
Expand All @@ -93,6 +99,7 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {

bool _zoomEnabled = true;
bool _hasNavigationDelegate = false;
bool _progressObserverSet = false;

final Map<String, WKScriptMessageHandler> _scriptMessageHandlers =
<String, WKScriptMessageHandler>{};
Expand Down Expand Up @@ -452,17 +459,20 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {
await _resetUserScripts(removedJavaScriptChannels: javascriptChannelNames);
}

Future<void> _setHasProgressTracking(bool hasProgressTracking) {
Future<void> _setHasProgressTracking(bool hasProgressTracking) async {
if (hasProgressTracking) {
return webView.addObserver(
_progressObserverSet = true;
await webView.addObserver(
webView,
keyPath: 'estimatedProgress',
options: <NSKeyValueObservingOptions>{
NSKeyValueObservingOptions.newValue,
},
);
} else {
return webView.removeObserver(webView, keyPath: 'estimatedProgress');
} else if (_progressObserverSet) {
// Calls to removeObserver before addObserver causes a crash.
_progressObserverSet = false;
await webView.removeObserver(webView, keyPath: 'estimatedProgress');
}
}

Expand Down Expand Up @@ -593,6 +603,16 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {

return value.toString();
}

void _dispose() {
if (_progressObserverSet) {
_progressObserverSet = false;
// It is recommended by Apple's documentation that this should be called
// before the WebView is deallocated:
// https://developer.apple.com/documentation/objectivec/nsobject/1408054-removeobserver?language=objc
webView.removeObserver(webView, keyPath: 'estimatedProgress');
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused; the webview is its own observer?

If so I'm not clear what removing the observer here would accomplish, since there can't be a call to a dangling pointer if it's the same object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just tested it and you are correct. There is no error when the WebView is deallocated. I went ahead and removed the dispose method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I think I originally misunderstood the documentation. It was recommending to remove the object that was added as an observer because notifications would still be sent to it if the observed object was still accessible. I understand it now.

}
}
}

/// Handles constructing objects and calling static methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,16 @@ void main() {

verify(mockCallbacksHandler.onProgress(32));
});

testWidgets('progress observer is not removed without being set first',
(WidgetTester tester) async {
await buildWidget(tester, hasProgressTracking: false);

verifyNever(mockWebView.removeObserver(
mockWebView,
keyPath: 'estimatedProgress',
));
});
});

group('JavascriptChannelRegistry', () {
Expand Down