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

Collection was Modified crash in didFinishNavigation (iOS) #130

Open
nlapham opened this issue Oct 8, 2019 · 0 comments
Open

Collection was Modified crash in didFinishNavigation (iOS) #130

nlapham opened this issue Oct 8, 2019 · 0 comments

Comments

@nlapham
Copy link

nlapham commented Oct 8, 2019

Hi,

My production app's top crash is caused by the foreach looping in Xam.Plugin.WebView.iOS -> FormsNavigationDelegate.cs the DidFinishNavigation. There are callbacks we add when the page is finished loading (it has to be once its finished) and this race condition of adding and looping over the callbacks causes intermittent crashing. I forked the repo and made the change for our app, but realized I do not have permissions to make a PR for this repo.

Please update the delegate to the following, or provide insight if you disagree.

The real change is making a copy of the callback dictionaries before looping (.ToList())

        [Export("webView:didFinishNavigation:")]
        public async override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
			if (Reference == null || !Reference.TryGetTarget(out FormsWebViewRenderer renderer)) return;
			if (renderer.Element == null) return;

            renderer.Element.HandleNavigationCompleted(webView.Url.ToString());
            await renderer.OnJavascriptInjectionRequest(FormsWebView.InjectedFunction);

            if (renderer.Element.EnableGlobalCallbacks)
            {
                var globalCallbacks = FormsWebView.GlobalRegisteredCallbacks.ToList();
                foreach (var function in globalCallbacks)
                {
                    await renderer.OnJavascriptInjectionRequest(FormsWebView.GenerateFunctionScript(function.Key));
                }
            }

            var localCallbacks = renderer.Element.LocalRegisteredCallbacks.ToList();
            foreach (var function in localCallbacks)
            {
                await renderer.OnJavascriptInjectionRequest(FormsWebView.GenerateFunctionScript(function.Key));
            }

            renderer.Element.CanGoBack = webView.CanGoBack;
            renderer.Element.CanGoForward = webView.CanGoForward;
            renderer.Element.Navigating = false;
            renderer.Element.HandleContentLoaded();
        }
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

1 participant