ReactiveUI icon indicating copy to clipboard operation
ReactiveUI copied to clipboard

Navigating Back crashes in Some scenarios Using ReactiveUI Tabbed Page. Unable to figure it out

Open Lavankumarkalimisetty opened this issue 5 years ago • 6 comments

Error: Unhandled Exception System.Exception: CurrentDomainOnUnhandledException ---> System.InvalidCastException: Specified cast is not valid.\n at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)\n at ReactiveUI.XamForms.ReactiveTabbedPage1[TViewModel].ReactiveUI.IViewFor.set_ViewModel (System.Object value) [0x00000] in d:\\a\\1\\s\\src\\ReactiveUI.XamForms\\ReactiveTabbedPage.cs:45 \n at ReactiveUI.XamForms.RoutedViewHost+<>c__DisplayClass1_0.<.ctor>b__18 (System.Reactive.EventPattern1[TEventArgs] _) [0x0003d] in d:\a\1\s\src\ReactiveUI.XamForms\RoutedViewHost.cs:143 \n at System.Reactive.AnonymousSafeObserver1[T].OnNext (T value) [0x0000a] in D:\\a\\1\\s\\Rx.NET\\Source\\src\\System.Reactive\\AnonymousSafeObserver.cs:43 \n at System.Reactive.Sink1[TTarget].ForwardOnNext (TTarget value) [0x00000] in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Internal\Sink.cs:49 \n at System.Reactive.Linq.ObservableImpl.Where1+Predicate+_[TSource].OnNext (TSource value) [0x0001e] in D:\\a\\1\\s\\Rx.NET\\Source\\src\\System.Reactive\\Linq\\Observable\\Where.cs:54 \n at System.Reactive.Subjects.Subject1[T].OnNext (T value) [0x0002b] in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\Subject.cs:148 \n at System.Reactive.Linq.ObservableImpl.FromEventPattern+Impl`2+<>c__DisplayClass3_0[TDelegate,TEventArgs].<GetHandler>b__0 (System.Object sender, TEventArgs eventArgs) [0x00000] in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\FromEventPattern.cs:44 \n at Xamarin.Forms.NavigationPage.Xamarin.Forms.INavigationPageController.RemoveAsyncInner (Xamarin.Forms.Page page, System.Boolean animated, System.Boolean fast) [0x00117] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:364 \n at Xamarin.Forms.Platform.iOS.NavigationRenderer.UpdateFormsInnerNavigation (Xamarin.Forms.Page pageBeingRemoved) [0x00072] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:827 \n at Xamarin.Forms.Platform.iOS.NavigationRenderer+ParentingViewController.DidMoveToParentViewController (UIKit.UIViewController parent) [0x0003a] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:1490 \n at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 \n at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.1.31/src/Xamarin.iOS/Foundation/NSAction.cs:178 \n--- End of stack trace from previous location where exception was thrown ---\n\n at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)\n at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.1.31/src/Xamarin.iOS/UIKit/UIApplication.cs:86 \n at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.1.31/src/Xamarin.iOS/UIKit/UIApplication.cs:65 \n at APP.iOS.Application.Main (System.String[] args) [0x00001] in /Users/A/APP Latest/Source/APP/APP.iOS/Main.cs:17 \n --- End of inner exception stack trace ---"

Lavankumarkalimisetty avatar Sep 08 '20 15:09 Lavankumarkalimisetty

Kindly help me to figure it out!! we had release last week due this issue we are unable to release it to customer.

TIA

Lavankumarkalimisetty avatar Sep 08 '20 15:09 Lavankumarkalimisetty

Looks to me the type of the view model is not the same as the type in the ReactiveTabbedPage at a glance. The error message could be improved on our end to make it more obvious.

glennawatson avatar Sep 08 '20 22:09 glennawatson

Scenario will be like stated:

I have landing page which is tabbed page. This exception is thrown when following action is performed

  1. When I navigate to a content page from toolbar
  2. When I press the back Button on the toolbar, I get this exception

My View model constructor is getting called before the exception occurs.

Issue occurs only in iOS devices.

Lavankumarkalimisetty avatar Sep 09 '20 06:09 Lavankumarkalimisetty

What's the ViewModel of the control being set to when you navigate back?

You can use SourceLink https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/sourcelink to tell most likely.

Best practice recommendations for using Source Link to improve debugging for .NET libraries.

glennawatson avatar Sep 09 '20 06:09 glennawatson

I've experienced similar issues. Somewhere in a new version of iOS (i think) they've build functionality to navigate back multiple pages at once. By long pressing the back, you get a popup window to pick a page to go back to.

When i navigate back multiple pages i get the same exception as above. I've narrowed it down the the following piece of code in the RoutedViewHost.cs.

https://github.com/reactiveui/ReactiveUI/blob/0518c2bc9e03328712bf1fb2e35bca3dfe5f9885/src/ReactiveUI.XamForms/RoutedViewHost.cs#L168-L173

When I have 4 pages on the navigation stack, so i am on the 4th page, and i navigate back to the first page. The page is the page from which I navigated, so the 4th page, and the viewmodel is the viewmodel for the previous page, so the 3rd page's viewmodel.

I'm not sure what this piece of code is supposed to do but I presume it has something to do with the android back button by reading the comments:

// NB: Catch when the user hit back as opposed to the application
// requesting Back via NavigateBack

Because the project I'm working on is iOS only I just removed this piece of code and it works like expected now.

I don't know if this is related to the question above but I hope it helps narrowing it down.

pdeboer-it avatar Mar 09 '21 12:03 pdeboer-it

I am experiencing a very similar issue. My main page is a ReactiveTabbedPage but the crash occurs when navigating back from a content page.

2022-04-20 12:01:12.574 +02:00 [FTL]<> Unhandled exception occured!
System.InvalidCastException: Specified cast is not valid.
  at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
  at ReactiveUI.XamForms.ReactiveContentPage`1[TViewModel].ReactiveUI.IViewFor.set_ViewModel (System.Object value) <0x1050b1aa0 + 0x00033> in <ca468454e1744d1db8a2c1c25667b2d4#9a03be62148982fb460c08ba0784e7f5>:0 
  at ReactiveUI.XamForms.RoutedViewHost+<>c__DisplayClass1_0.<.ctor>b__17 (System.Reactive.Unit _) <0x1050b81c0 + 0x0019b> in <ca468454e1744d1db8a2c1c25667b2d4#9a03be62148982fb460c08ba0784e7f5>:0 
  at System.Reactive.AnonymousSafeObserver`1[T].OnNext (T value) <0x104a45b80 + 0x00043> 16413 in <3ad0cc868be642ac8d0c22906bb12ddb#9a03be62148982fb460c08ba0784e7f5>:0 
  at System.Reactive.Sink`1[TTarget].ForwardOnNext (TTarget value) <0x104a24ce0 + 0x00047> 15603 in <3ad0cc868be642ac8d0c22906bb12ddb#9a03be62148982fb460c08ba0784e7f5>:0 
  at System.Reactive.Linq.ObservableImpl.Where`1+Predicate+_[TSource].OnNext (TSource value) <0x1049bfe60 + 0x000ff> in <3ad0cc868be642ac8d0c22906bb12ddb#9a03be62148982fb460c08ba0784e7f5>:0 
  at System.Reactive.Subjects.Subject`1[T].OnNext (T value) <0x104a0b180 + 0x000fb> 14838 in <3ad0cc868be642ac8d0c22906bb12ddb#9a03be62148982fb460c08ba0784e7f5>:0 
  at ReactiveUI.XamForms.RoutedViewHost+<>c__DisplayClass1_1.<.ctor>g__Handler|18 (System.Object sender, Xamarin.Forms.NavigationEventArgs e) <0x1050b8ec0 + 0x00033> in <ca468454e1744d1db8a2c1c25667b2d4#9a03be62148982fb460c08ba0784e7f5>:0 
  at Xamarin.Forms.NavigationPage.Xamarin.Forms.INavigationPageController.RemoveAsyncInner (Xamarin.Forms.Page page, System.Boolean animated, System.Boolean fast) <0x102d15f20 + 0x0030b> in <62e3629c74b84e3d834046331d2bb5f8#9a03be62148982fb460c08ba0784e7f5>:0 
  at Xamarin.Forms.Platform.iOS.NavigationRenderer.UpdateFormsInnerNavigation (Xamarin.Forms.Page pageBeingRemoved) <0x103adc380 + 0x002a3> in <bcf33063bc7d4acc8120df932dca3072#9a03be62148982fb460c08ba0784e7f5>:0 
  at Xamarin.Forms.Platform.iOS.NavigationRenderer+ParentingViewController.DidMoveToParentViewController (UIKit.UIViewController parent) <0x103ad90b0 + 0x001a3> in <bcf33063bc7d4acc8120df932dca3072#9a03be62148982fb460c08ba0784e7f5>:0 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) <0x102973a20 + 0x00053> in <2ac4b0e4db61471390528bfab2c52fb6#9a03be62148982fb460c08ba0784e7f5>:0 
  at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () <0x103840920 + 0x0002b> in <cb0b2066644843c591612869ad7b412b#9a03be62148982fb460c08ba0784e7f5>:0 
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x10387fa70 + 0x0005f> in <cb0b2066644843c591612869ad7b412b#9a03be62148982fb460c08ba0784e7f5>:0 
  at MyApp.iOS.Application.Main (System.String[] args) <0x1027d2ab0 + 0x00023> in <5cd84a3e0b6040cea860b46cf2ebf09e#9a03be62148982fb460c08ba0784e7f5>:0 

This issue is very similar to #2317 and #2286 (and their respective PRs #2331 and #2315). However the fixes mentioned in those issues seem to be part of ReactiveUI since then, yet the issue sporadically appears (not on every navigate).

Was wondering if anyone found another solution besides what was mentioned by @pdeboer-it ? It does only happen on iOS it seems, but when navigating back a single page not multiple pages.

scheidtdav avatar Apr 22 '22 11:04 scheidtdav

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

github-actions[bot] avatar Mar 07 '23 00:03 github-actions[bot]