Prism icon indicating copy to clipboard operation
Prism copied to clipboard

[XF] Random navigation service crash "Only the original thread that created a view hierarchy can touch its views"

Open JeffDalby opened this issue 10 years ago • 9 comments

Using XF 2.1.0.6524, only on Android KitKat (VM and real device) (possibly earlier but we've not tested for that). Seems to happen more often on GoBack, but it's happened on normal modal navigation as well. We've tried wrapping the navigation service calls in Device.BeginInvokeOnMainThread, but it hasn't helped. It doesn't happen every time, but bouncing around between pages with a navigation model single home page that opens other pages modally,and then uses go back to return to the home page.

No clue if this is a Xamarin vs PRISM issue, just that since it's happening calling GoBack, there's not much more I can do to debug. Closest I can get to a stack trace (had to use hockey app to grab it)

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6024) at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:853) at android.view.ViewGroup.invalidateChildFast(ViewGroup.java:4441) at android.view.View.invalidateViewProperty(View.java:10972) at android.view.View.setAlpha(View.java:9865) at dalvik.system.NativeStart.run(Native Method)

JeffDalby avatar Mar 07 '16 19:03 JeffDalby

Just tested on JellyBean, and it's got the same issue.

JeffDalby avatar Mar 07 '16 20:03 JeffDalby

Very strange. As you can see, all Prism does is call Xamarin.Forms Pop:

https://github.com/PrismLibrary/Prism/blob/master/Source/Xamarin/Prism.Forms/Navigation/PageNavigationService.cs#L41

It's not doing anything with threading. Can you debug through source and see where the exception is thrown?

brianlagunas avatar Mar 07 '16 20:03 brianlagunas

Yeah, happy to help how I can. Let me grab it and get setup for that. I'll update when I've got some info.

JeffDalby avatar Mar 07 '16 20:03 JeffDalby

Well it's dying in DoPush now, I can't get it to crash in GoBack after changing GoBack to:

Device.BeginInvokeOnMainThread(async () => {

                var poppedPage = await DoPop(page.Navigation, useModalForDoPop, animated);

                if (poppedPage != null)
                    OnNavigatedTo(previousPage, segmentParameters);
            });

I've got a feeling that change isn't helping and is just a coincidence, since the crash has always been random. Currently I can crash it by navigating back and forth anywhere from 2 to 10 times or so, always in DoPush

if (useModalNavigation) await currentPage.Navigation.PushModalAsync(page, animated);

Trying to force PushModalAsync onto the main thread with BeginInvokeOnMainThread doesn't change anything. It crashes prior OnNavigatedTo running (since DoPush is being called as navigationAction() within DoNavigateAction) so it can't be anything in there.

Seems to me that it's Xamarin's code, but I have no idea if they'd say the cause is because it somehow is not being called on the thread that created the view in the first place.

And to throw something completely beyond my ability to understand, if I put a GC.Collect() in the OnNavigatedTo on any viewmodel it will also crash the app with the exact same error. I had that on one of our pages per Xamarin support because garbage collection cleaning things up before we had to load a bunch of data, but since then we've done some optimization and don't really need it anymore. Still I don't get how garbage collection results in the same error.

Do you have a good relationship with the Xamarin guys these days, or would you prefer I open a support ticket with them?

JeffDalby avatar Mar 08 '16 00:03 JeffDalby

I do have a relationship with Xamarin, but they have been a little non-responsive sine the buy out with MS and with the up coming Evolve event. I have two outstanding bugs still waiting to be fixed. What I like to do is recreate the bug using standard Xamarin Forms code and then send them that with the bug so they can't say it is Prism. So far it has never been Prism :)

By the way, the ViewModel is created on the same thread as the View. I would submit the bug and then post the bug link here so that we can track it's progress.

brianlagunas avatar Mar 08 '16 00:03 brianlagunas

I've got ticket with them for an iOS defect. They responded today asking me to send them an app that duplicates it, I'll just try to create one that does both. Two birds, one stone and all that. I'll post the results here when I hear back from them.

JeffDalby avatar Mar 08 '16 00:03 JeffDalby

Close this out, as suspected a Xamarin Issue, though not what I thought it was. You've got to love the lack of info you get from certain Android crashes with Xamarin. While the crash was in the navigation, the issue was in a custom control, which happened to be the only thing not commented out for testing because it was an animated image button I use as the Go back button.

On pre-Lollipop Android if you have a custom control that implements ICommand the CanExecuteChanged method fires on a background thread.....sometimes. This doesn't happen on 5.x or 6.x.

JeffDalby avatar Mar 10 '16 12:03 JeffDalby

Thanks for the update.

brianlagunas avatar Mar 10 '16 16:03 brianlagunas

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 31 '20 22:01 lock[bot]