SwiftUI - Ipad crashes when form elements disappear

If you have a form like the below, to show some fields just when a specific option of a Picker is selected (so it appears/disappears based on the picker selected option) if you are running an Ipad and you are inside (keyboard opened) of any of the fields that will disappear if you select the other Picker element, once doing it the app crashes:


Picker(selection: $createAccount, label: Text("")) {
     Text("New Account")
          .tag(0)
                   
     Text("Existing Account")
          .tag(1)
}
.pickerStyle(SegmentedPickerStyle())
    
Form {
     Section(header: Text("Personal information")) {
          if (self.createAccount == 0) {
               TextField("Name", text: self.$name)
          }
     }
}


This happens if you play with .disabled too.


Does anyone have a workaround? Perhaps this is a known bug?


Crash trace


Exception Type:        EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes:       0x0000000000000001, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY


Termination Signal:    Illegal instruction: 4
Termination Reason:    Namespace SIGNAL, Code 0x4
Terminating Process:   exc handler [17379]


Application Specific Information:
CoreSimulator 681.15 - Device: iPad Pro (12.9-inch) (3rd generation) (DC77C3DF-407A-4DCD-B511-FAB8212E03EA) - Runtime: iOS 13.2 (17B84) - DeviceType: iPad Pro (12.9-inch) (3rd generation)


Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   com.apple.SwiftUI             0x00007fff2c19a4d1 ViewGraph.runTransaction(in:) + 273
1   com.apple.SwiftUI             0x00007fff2c19a8b7 closure #1 in ViewGraph.updateOutputs(at:) + 103
2   com.apple.SwiftUI             0x00007fff2c19a571 ViewGraph.updateOutputs(at:) + 145
3   com.apple.SwiftUI             0x00007fff2c4ed609 closure #1 in closure #1 in ViewRendererHost.render(interval:updateDisplayList:) + 1001
4   com.apple.SwiftUI             0x00007fff2c4ed01a closure #1 in ViewRendererHost.render(interval:updateDisplayList:) + 634
5   com.apple.SwiftUI             0x00007fff2c4e1074 ViewRendererHost.render(interval:updateDisplayList:) + 436
6   com.apple.SwiftUI             0x00007fff2c67ce22 _UIHostingView.layoutSubviews() + 226
7   com.apple.SwiftUI             0x00007fff2c67ce45 @objc _UIHostingView.layoutSubviews() + 21
8   com.apple.UIKitCore           0x00007fff47d34cfd -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
9   com.apple.QuartzCore           0x00007fff2b138d41 -[CALayer layoutSublayers] + 255
10  com.apple.QuartzCore           0x00007fff2b13ef33 CA::Layer::layout_if_needed(CA::Transaction*) + 517
11  com.apple.UIKitCore           0x00007fff47d1fe75 -[UIView(Hierarchy) layoutBelowIfNeeded] + 980
12  com.apple.UIKitCore           0x00007fff470f2f0a -[_UISheetPresentationController _sheetLayoutInfoLayout:] + 48
13  com.apple.UIKitCore           0x00007fff470f06b2 -[_UISheetLayoutInfo _layout] + 360
14  com.apple.UIKitCore           0x00007fff470f3e33 __74-[_UISheetPresentationController _handleKeyboardNotification:aboutToHide:]_block_invoke_2 + 52
15  com.apple.UIKitCore           0x00007fff47d28626 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 528
16  com.apple.UIKitCore           0x00007fff47d28bd5 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 99
17  com.apple.UIKitCore           0x00007fff470f3df9 __74-[_UISheetPresentationController _handleKeyboardNotification:aboutToHide:]_block_invoke.294 + 103
18  com.apple.UIKitCore           0x00007fff47d26ef3 +[UIView(Animation) _performWithAnimation:] + 87
19  com.apple.UIKitCore           0x00007fff470f3d3a -[_UISheetPresentationController _handleKeyboardNotification:aboutToHide:] + 584
20  com.apple.UIKitCore           0x00007fff470f3f4e -[_UISheetPresentationController _keyboardAboutToChangeFrame:] + 63
21  com.apple.CoreFoundation       0x00007fff23b7936c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
22  com.apple.CoreFoundation       0x00007fff23b787e5 _CFXRegistrationPost1 + 421
23  com.apple.CoreFoundation       0x00007fff23b78551 ___CFXNotificationPost_block_invoke + 193
24  com.apple.CoreFoundation       0x00007fff23c76873 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1811
25  com.apple.CoreFoundation       0x00007fff23b77ea6 _CFXNotificationPost + 950
26  com.apple.Foundation           0x00007fff2572bc07 -[NSNotificationCenter postNotificationName:object:userInfo:] + 59
27  com.apple.UIKitCore           0x00007fff4762482e __68-[UIInputWindowController postValidatedStartNotifications:withInfo:]_block_invoke + 1026
28  com.apple.UIKitCore           0x00007fff47624425 -[UIInputWindowController postValidatedStartNotifications:withInfo:] + 137
29  com.apple.UIKitCore           0x00007fff47627b24 __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke.862 + 571
30  com.apple.UIKitCore           0x00007fff47d28626 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 528
31  com.apple.UIKitCore           0x00007fff47d28b4b +[UIView(UIViewAnimationWithBlocks) _animateWithDuration:delay:options:animations:start:completion:] + 116
32  com.apple.UIKitCore           0x00007fff4762758d -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] + 2045
33  com.apple.UIKitCore           0x00007fff4762cbcf -[UIInputWindowController setInputViewSet:] + 922
34  com.apple.UIKitCore           0x00007fff47626a57 -[UIInputWindowController performOperations:withAnimationStyle:] + 47
35  com.apple.UIKitCore           0x00007fff4731755e -[UIInputResponderController setKeyWindowSceneInputViews:animationStyle:] + 2175
36  com.apple.UIKitCore           0x00007fff47316cba -[UIInputResponderController setInputViews:animationStyle:] + 195
37  com.apple.UIKitCore           0x00007fff47317dde -[UIInputResponderController setInputViews:animated:] + 88
38  com.apple.UIKitCore           0x00007fff47317e45 -[UIInputResponderController setInputViews:] + 68
39  com.apple.UIKitCore           0x00007fff47315a55 -[UIInputResponderController _reloadInputViewsForKeyWindowSceneResponder:] + 3281
40  com.apple.UIKitCore           0x00007fff47314d68 -[UIInputResponderController _reloadInputViewsForResponder:] + 144
41  com.apple.UIKitCore           0x00007fff4787d242 -[UIResponder _finishResignFirstResponder] + 435
42  com.apple.UIKitCore           0x00007fff47b44068 -[UITextField _finishResignFirstResponder] + 48
43  com.apple.UIKitCore           0x00007fff4787d357 -[UIResponder resignFirstResponder] + 275
44  com.apple.UIKitCore           0x00007fff47b43ee2 -[UITextField resignFirstResponder] + 93
45  com.apple.UIKitCore           0x00007fff47a21694 -[UITableView _resignFirstResponderInDeletedSectionOrRow:] + 503
46  com.apple.UIKitCore           0x00007fff47a21a29 -[UITableView _updateWithItems:updateSupport:] + 771
47  com.apple.UIKitCore           0x00007fff47a1a220 -[UITableView _endCellAnimationsWithContext:] + 17188
48  com.apple.UIKitCore           0x00007fff47a34057 -[UITableView endUpdatesWithContext:] + 112
49  com.apple.UIKitCore           0x00007fff47a34203 -[UITableView _performBatchUpdates:withContext:completion:] + 253
50  com.apple.UIKitCore           0x00007fff47a3431b -[UITableView performBatchUpdates:completion:] + 98
51  com.apple.SwiftUI             0x00007fff2c265f66 UpdateCoalescingTableView.performBatchUpdates(_:completion:) + 470
52  com.apple.SwiftUI             0x00007fff2c2660ef @objc UpdateCoalescingTableView.performBatchUpdates(_:completion:) + 175
53  com.apple.SwiftUI             0x00007fff2c26860c ListCoreCoordinator.updateUITableView(_:from:to:) + 1692
54  com.apple.SwiftUI             0x00007fff2c266f2d _ListCore.updateUIView(_:context:) + 973
55  com.apple.SwiftUI             0x00007fff2c6294d1 PlatformViewRepresentableAdaptor.updateViewProvider(_:context:) + 289
56  com.apple.SwiftUI             0x00007fff2c2de29b closure #1 in PlatformViewChild.update(context:) + 2315
57  com.apple.SwiftUI             0x00007fff2c2d7f96 PlatformViewChild.update(context:) + 310
58  com.apple.SwiftUI             0x00007fff2c2df600 protocol witness for static UntypedAttribute._update(_:graph:attribute:) in conformance PlatformViewChild<A> + 32
59  com.apple.AttributeGraph       0x00007fff2f8bbc69 partial apply + 25
60  com.apple.AttributeGraph       0x00007fff2f8a3ac5 AG::Graph::UpdateStack::update() + 1111
61  com.apple.AttributeGraph       0x00007fff2f8a3d83 AG::Graph::update_attribute(unsigned int, bool) + 377
62  com.apple.AttributeGraph       0x00007fff2f8a89a1 AG::Subgraph::update(unsigned int) + 929
63  com.apple.SwiftUI             0x00007fff2c19a4a0 ViewGraph.runTransaction(in:) + 224
64  com.apple.SwiftUI             0x00007fff2c19a270 closure #1 in ViewGraph.flushTransactions() + 256
65  com.apple.SwiftUI             0x00007fff2c199f0f ViewGraph.flushTransactions() + 223
66  com.apple.SwiftUI             0x00007fff2c19a08f closure #1 in closure #1 in ViewGraph.asyncTransaction<A>(_:mutation:style:) + 15
67  com.apple.SwiftUI             0x00007fff2c198219 specialized thunk for @callee_guaranteed (@guaranteed ViewGraph) -> () + 9
68  com.apple.SwiftUI             0x00007fff2c4e0817 ViewRendererHost.updateViewGraph<A>(body:) + 71
69  com.apple.SwiftUI             0x00007fff2c4e07c3 protocol witness for ViewGraphDelegate.updateViewGraph<A>(body:) in conformance _UIHostingView<A1> + 19
70  com.apple.SwiftUI             0x00007fff2c19a06a closure #1 in ViewGraph.asyncTransaction<A>(_:mutation:style:) + 122
71  com.apple.SwiftUI             0x00007fff2c1b7cec thunk for @escaping @callee_guaranteed () -> () + 12
72  com.apple.SwiftUI             0x00007fff2bffc051 partial apply for thunk for @escaping @callee_guaranteed () -> () + 17
73  com.apple.SwiftUI             0x00007fff2bffbf77 static NSRunLoop.flushObservers() + 119
74  com.apple.SwiftUI             0x00007fff2bffbef9 closure #1 in static NSRunLoop.addObserver(_:) + 9
75  com.apple.SwiftUI             0x00007fff2bffbfeb @objc closure #1 in static NSRunLoop.addObserver(_:) + 43
76  com.apple.CoreFoundation       0x00007fff23bb1617 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
77  com.apple.CoreFoundation       0x00007fff23bac0ae __CFRunLoopDoObservers + 430
78  com.apple.CoreFoundation       0x00007fff23bac72a __CFRunLoopRun + 1514
79  com.apple.CoreFoundation       0x00007fff23****16 CFRunLoopRunSpecific + 438
80  com.apple.GeoServices         0x00007fff38438bb0 GSEventRunModal + 65
81  com.apple.UIKitCore           0x00007fff4784fb68 UIApplicationMain + 1621
82  com.selligent.Parana-Digital   0x000000010b8d239b main + 75 (AppDelegate.swift:14)
83  libdyld.dylib                 0x00007fff51a1dc25 start + 1


Thread 1:
0   libsystem_pthread.dylib       0x00007fff51c0156c start_wqthread + 0


Thread 2:: com.apple.uikit.eventfetch-thread
0   libsystem_kernel.dylib         0x00007fff51b54166 mach_msg_trap + 10
1   libsystem_kernel.dylib         0x00007fff51b546cc mach_msg + 60
2   com.apple.CoreFoundation       0x00007fff23bb1aa5 __CFRunLoopServiceMachPort + 197
3   com.apple.CoreFoundation       0x00007fff23bac7c7 __CFRunLoopRun + 1671
4   com.apple.CoreFoundation       0x00007fff23****16 CFRunLoopRunSpecific + 438
5   com.apple.Foundation           0x00007fff2574b87f -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 211
6   com.apple.Foundation           0x00007fff2574baf2 -[NSRunLoop(NSRunLoop) runUntilDate:] + 72
7   com.apple.UIKitCore           0x00007fff478f21ac -[UIEventFetcher threadMain] + 138
8   com.apple.Foundation           0x00007fff257617b7 __NSThread__start__ + 1047
9   libsystem_pthread.dylib       0x00007fff51c04d36 _pthread_start + 125
10  libsystem_pthread.dylib       0x00007fff51c0158f thread_start + 15


Thread 3:
0   libsystem_pthread.dylib       0x00007fff51c0156c start_wqthread + 0


Thread 4:
0   libsystem_pthread.dylib       0x00007fff51c0156c start_wqthread + 0


Thread 5:
0   libsystem_pthread.dylib       0x00007fff51c0156c start_wqthread + 0


Thread 6:
0   libsystem_pthread.dylib       0x00007fff51c0156c start_wqthread + 0


Best,

Answered by Jim Dovey in 393969022

This sounds like a bug in SwiftUI to me, and one they'll want to jump on quickly. Please grab your logs and submit an error via feedbackassistant.apple.com.

My suspicion is that this is a bug in SwiftUI; the text field should resign first responder status as part of the view hierarchy update, but perhaps that isn't happening. Can you attach the crash log info here? That would help to determine if that's really the case.

Try adding an else statement with an EmptyView()


  if (self.createAccount == 0) { 
     TextField("Name", text: self.$name) 
  }
  else {
     EmptyView()
  }


Or a .hidden() TextField:


  if (self.createAccount == 0) { 
     TextField("Name", text: self.$name) 
  }
  else {
     TextField("Name", text: self.$name).hidden()
  }


I believe the reason for the crash is that the Section is expecting a View inside of it, but I could be mistaken.

kdion4891 any of the 2 solutions worked 😟, original code fails just for iPad though, fine for iPhone

Jim Dovey trying to add stack trace in the original post, waiting for a moderator

So the text field is indeed resigning first responder status:


41  com.apple.UIKitCore           0x00007fff4787d242 -[UIResponder _finishResignFirstResponder] + 435  
42  com.apple.UIKitCore           0x00007fff47b44068 -[UITextField _finishResignFirstResponder] + 48 


Further up the stack, though, it's moving things around and performing layout of a sheet view. This appears to be hosting some SwiftUI view, and the app is crashing when it attempts to update the layout graph. Judging by the exception (illegal instruction) and the fact that it's happening in a function called runTransaction(in:), I'm inclined to believe there's already a transaction in process here, and this is a software crash (i.e. a fatalError() or failed precondition() call). The stack frame you posted ends at frame 58, but it looks like there should be more. If you look, are there two instances of runTransaction() in the full crash log?

Yeah there is another instance of runTransaction(), I'm adding the full trace in the first post again.


Below the part that was missing


59  com.apple.AttributeGraph      
0x00007fff2f8bbc69 partial apply + 25
60  com.apple.AttributeGraph      
0x00007fff2f8a3ac5 AG::Graph::UpdateStack::update() + 1111
61  com.apple.AttributeGraph      
0x00007fff2f8a3d83 AG::Graph::update_attribute(unsigned int, bool) + 377
62  com.apple.AttributeGraph      
0x00007fff2f8a89a1 AG::Subgraph::update(unsigned int) + 929
63  com.apple.SwiftUI            
0x00007fff2c19a4a0 ViewGraph.runTransaction(in:) + 224
64  com.apple.SwiftUI            
0x00007fff2c19a270 closure #1 in ViewGraph.flushTransactions() + 256
65  com.apple.SwiftUI            
0x00007fff2c199f0f ViewGraph.flushTransactions() + 223
66  com.apple.SwiftUI            
0x00007fff2c19a08f closure #1 in closure #1 in ViewGraph.asyncTransaction<A>(_:mutation:style:) + 15
67  com.apple.SwiftUI            
0x00007fff2c198219 specialized thunk for @callee_guaranteed (@guaranteed ViewGraph) -> () + 9
68  com.apple.SwiftUI            
0x00007fff2c4e0817 ViewRendererHost.updateViewGraph<A>(body:) + 71
69  com.apple.SwiftUI            
0x00007fff2c4e07c3 protocol witness for ViewGraphDelegate.updateViewGraph<A>(body:) in conformance _UIHostingView<A1> + 19
70  com.apple.SwiftUI            
0x00007fff2c19a06a closure #1 in ViewGraph.asyncTransaction<A>(_:mutation:style:) + 122
71  com.apple.SwiftUI            
0x00007fff2c1b7cec thunk for @escaping @callee_guaranteed () -> () + 12
72  com.apple.SwiftUI            
0x00007fff2bffc051 partial apply for thunk for @escaping @callee_guaranteed () -> () + 17
73  com.apple.SwiftUI            
0x00007fff2bffbf77 static NSRunLoop.flushObservers() + 119
74  com.apple.SwiftUI            
0x00007fff2bffbef9 closure #1 in static NSRunLoop.addObserver(_:) + 9
75  com.apple.SwiftUI            
0x00007fff2bffbfeb @objc closure #1 in static NSRunLoop.addObserver(_:) + 43
76  com.apple.CoreFoundation      
0x00007fff23bb1617 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
77  com.apple.CoreFoundation      
0x00007fff23bac0ae __CFRunLoopDoObservers + 430
78  com.apple.CoreFoundation      
0x00007fff23bac72a __CFRunLoopRun + 1514
79  com.apple.CoreFoundation      
0x00007fff23****16 CFRunLoopRunSpecific + 438
80  com.apple.GeoServices        
0x00007fff38438bb0 GSEventRunModal + 65
81  com.apple.UIKitCore          
0x00007fff4784fb68 UIApplicationMain + 1621
82  com.selligent.Parana-Digital  
0x000000010b8d239b main + 75 (AppDelegate.swift:14)
83  libdyld.dylib                
0x00007fff51a1dc25 start + 1



Thread 1:
0   libsystem_pthread.dylib      
0x00007fff51c0156c start_wqthread + 0



Thread 2:: com.apple.uikit.eventfetch-thread
0   libsystem_kernel.dylib        
0x00007fff51b54166 mach_msg_trap + 10
1   libsystem_kernel.dylib        
0x00007fff51b546cc mach_msg + 60
2   com.apple.CoreFoundation      
0x00007fff23bb1aa5 __CFRunLoopServiceMachPort + 197
3   com.apple.CoreFoundation      
0x00007fff23bac7c7 __CFRunLoopRun + 1671
4   com.apple.CoreFoundation      
0x00007fff23****16 CFRunLoopRunSpecific + 438
5   com.apple.Foundation          
0x00007fff2574b87f -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 211
6   com.apple.Foundation          
0x00007fff2574baf2 -[NSRunLoop(NSRunLoop) runUntilDate:] + 72
7   com.apple.UIKitCore          
0x00007fff478f21ac -[UIEventFetcher threadMain] + 138
8   com.apple.Foundation          
0x00007fff257617b7 __NSThread__start__ + 1047
9   libsystem_pthread.dylib      
0x00007fff51c04d36 _pthread_start + 125
10  libsystem_pthread.dylib      
0x00007fff51c0158f thread_start + 15



Thread 3:
0   libsystem_pthread.dylib      
0x00007fff51c0156c start_wqthread + 0



Thread 4:
0   libsystem_pthread.dylib      
0x00007fff51c0156c start_wqthread + 0



Thread 5:
0   libsystem_pthread.dylib      
0x00007fff51c0156c start_wqthread + 0



Thread 6:
0   libsystem_pthread.dylib      
0x00007fff51c0156c start_wqthread + 0
Accepted Answer

This sounds like a bug in SwiftUI to me, and one they'll want to jump on quickly. Please grab your logs and submit an error via feedbackassistant.apple.com.

reported, just fyi the crash occurs as long as the Form is inside a Sheet, otherwise it seems to work fine

Thanks for your work on this Marc. I just wanted to echo: as of XCode 11.4, this is still an issue. TextFields with an active cursor seem to cause a crash when removed from a view within a "modal" sheet on iPad. Works fine on iPhone. I can't even try to play games with Application.shared to resign first responder as I have a share extension in my app, but all other attempts to workaround have failed.

SwiftUI - Ipad crashes when form elements disappear
 
 
Q