SwiftUI "pushing the same view controller instance" Exception

We've been working on a SwiftUI app that randomly crashes with an exception. When navigating from one view to another, a rare exception is thrown, maybe every 1 / 200 times or so:

<SwiftUI.UIKitNavigationController: 0x109888400> is pushing the same view controller instance (<_TtGC7SwiftUI32NavigationStackHostingControllerVS_7AnyView_: 0x10792d400>) more than once which is not supported and is most likely an error in the application : com.<companyName>.<appName>

We haven't coded anything to directly push an instance of a view controller outside of what SwiftUI is doing. It seems to happen when the user taps on a NavigationLink view. It happens both in simulator and on device. Does anyone know what might cause this?

Replies

Have you identified the concerned view ?

When you've found, add the following test in this View's body:

            .onAppear {
                print("View appeared!")
            }
            .onDisappear {
                print("View disappeared!")
            }

And tell what you see when bugging in simulator.

Does it occur when you navigate very rapidly ? If so, that would probably mean that SwiftUI had no time to pop the view before pushing again.

See old discussion here (not SwiftUI): https://stackoverflow.com/questions/7083124/pushing-the-same-view-controller-instance-more-than-once-is-not-supported-exce

  • I tried adding those print statements to onAppear and onDisappear for the view that was being navigated to, but neither printed.

Add a Comment

Here is the call stack:

*** First throw call stack:
(
	0   CoreFoundation                      0x00000001804ae138 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x0000000180087db4 objc_exception_throw + 56
	2   UIKitCore                           0x0000000184c11bb4 -[UINavigationController pushViewController:transition:forceImmediate:] + 3736
	3   UIKitCore                           0x0000000184c10bac -[UINavigationController pushViewController:animated:] + 756
	4   SwiftUI                             0x00000001cc954f7c OUTLINED_FUNCTION_4 + 109100
	5   SwiftUI                             0x00000001cc95c274 block_destroy_helper + 8052
	6   SwiftUI                             0x00000001cc949a54 OUTLINED_FUNCTION_4 + 62724
	7   SwiftUI                             0x00000001cc952ca0 OUTLINED_FUNCTION_4 + 100176
	8   SwiftUI                             0x00000001cc94e8f0 OUTLINED_FUNCTION_4 + 82848
	9   SwiftUI                             0x00000001cc1f7c2c OUTLINED_FUNCTION_14 + 28
	10  SwiftUI                             0x00000001cc7c0f58 OUTLINED_FUNCTION_16 + 15280
	11  SwiftUI                             0x00000001cc7c0ab0 OUTLINED_FUNCTION_16 + 14088
	12  SwiftUI                             0x00000001cc7bf25c OUTLINED_FUNCTION_16 + 7860
	13  SwiftUI                             0x00000001cc81c970 OUTLINED_FUNCTION_20 + 42792
	14  SwiftUI                             0x00000001cc81c9b8 OUTLINED_FUNCTION_20 + 42864
	15  UIKitCore                           0x000000018590a128 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1492
	16  QuartzCore                          0x000000018a1ae654 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
	17  QuartzCore                          0x000000018a1b940c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124
	18  QuartzCore                          0x000000018a0f2d30 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 460
	19  QuartzCore                          0x000000018a122aa8 _ZN2CA11Transaction6commitEv + 652
	20  QuartzCore                          0x000000018a123f7c _ZN2CA11Transaction25flush_as_runloop_observerEb + 68
	21  CoreFoundation                      0x000000018040de34 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
	22  CoreFoundation                      0x0000000180408838 __CFRunLoopDoObservers + 528
	23  CoreFoundation                      0x0000000180408cf0 __CFRunLoopRun + 968
	24  CoreFoundation                      0x0000000180408514 CFRunLoopRunSpecific + 572
	25  GraphicsServices                    0x000000018ef06ae4 GSEventRunModal + 160
	26  UIKitCore                           0x00000001853e8040 -[UIApplication _run] + 868
	27  UIKitCore                           0x00000001853ebcc8 UIApplicationMain + 124
	28  SwiftUI                             0x00000001cc1fcb68 OUTLINED_FUNCTION_65 + 492
	29  SwiftUI                             0x00000001cc1fca10 OUTLINED_FUNCTION_65 + 148
	30  SwiftUI                             0x00000001cbebb068 OUTLINED_FUNCTION_0 + 92
	31  <ourAppName>                        0x00000001026d1f44 $s16<ourAppName>0aB3AppV5$mainyyFZ + 40
	32  <ourAppName>                        0x00000001026d2744 main + 12
	33  dyld                                0x00000001036e9544 start_sim + 20
	34  ???                                 0x000000010385a0e0 0x0 + 4354056416
	35  ???                                 0x2619000000000000 0x0 + 2745225447859027968
)
libc++abi: terminating due to uncaught exception of type NSException