Crash while using UICollectionViewCompositionalLayout with pinned header views to the layout - ios

I've been noticing a crash with UICollectionViewCompositionalLayout while adding a sticky header to the collection view using NSCollectionLayoutBoundarySupplementaryItem through UICollectionViewCompositionalLayoutConfiguration.
These grid view header items are all pinned to the visible bounds via pinToVisibleBounds property to make them sticky while scrolling through rows.
Here's the crash
2022-04-09 00:28:56.335279-0700 *****[88274:5668805] *** Assertion failure in CGRect _UIPinnedFrameForFrameWithContainerFrameVisibleFrame(CGRect, CGRect, CGRect, NSRectAlignment)(), _UICollectionLayoutHelpers.m:688
2022-04-09 00:28:56.341980-0700 ******[88274:5668805] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Frame {{377, 0}, {0, 627}} does not intersect {{377, 0}, {0, 627}}'
*** First throw call stack:
(
0  CoreFoundation           0x000000010aa45d44 __exceptionPreprocess + 242
1  libobjc.A.dylib           0x0000000107671a65 objc_exception_throw + 48
2  Foundation             0x00000001089357d9 -[NSMutableDictionary(NSMutableDictionary) classForCoder] + 0
3  UIKitCore              0x00000001187a09cb _UIPinnedNonOverlappingFramesForContentFrameVisibleFrame + 3154
4  UIKitCore              0x00000001187b7ff4 -[_UICollectionLayoutAuxillaryItemSolver _solveForPinning:visibleRect:] + 5748
5  UIKitCore              0x00000001187824ed -[_UICollectionCompositionalLayoutSolver updatePinnedSectionSupplementaryItemsForVisibleBounds:] + 723
6  UIKitCore              0x000000011877e542 -[UICollectionViewCompositionalLayout _updatePinnedSectionSupplementaryItemsForCurrentVisibleBounds] + 381
7  UIKitCore              0x000000011877f148 -[UICollectionViewCompositionalLayout _solveForPinnedSupplementaryItemsIfNeededWithContext:] + 121
8  UIKitCore              0x000000011877a203 -[UICollectionViewCompositionalLayout invalidateLayoutWithContext:] + 802
9  UIKitCore              0x000000011888770d -[UICollectionViewLayout _invalidateLayoutUsingContext:] + 56
10 UIKitCore              0x00000001188202e5 -[UICollectionView setBounds:] + 757
11 UIKitCore              0x0000000119713ab2 -[UIScrollView setContentOffset:] + 1047
12 UIKitCore              0x0000000118839f2e -[UICollectionView setContentOffset:] + 42
13 UIKitCore              0x0000000119728b6e -[UIScrollView _smoothScrollSyncWithUpdateTime:] + 3152
14 UIKitCore              0x0000000119727b55 -[UIScrollView _smoothScrollWithUpdateTime:] + 313
15 UIKitCore              0x0000000119728f06 -[UIScrollView _smoothScrollDisplayLink:] + 613
16 QuartzCore             0x0000000113937474 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 932
17 QuartzCore             0x0000000113a369c6 _ZL22display_timer_callbackP12__CFMachPortPvlS1_ + 395
18 CoreFoundation           0x000000010a97eb42 __CFMachPortPerform + 157
19 CoreFoundation           0x000000010a9b3125 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
20 CoreFoundation           0x000000010a9b24cc __CFRunLoopDoSource1 + 617
21 CoreFoundation           0x000000010a9ac901 __CFRunLoopRun + 2420
22 CoreFoundation           0x000000010a9aba90 CFRunLoopRunSpecific + 562
23 GraphicsServices          0x0000000110617c8e GSEventRunModal + 139
24 UIKitCore              0x00000001191e490e -[UIApplication _run] + 928
25 UIKitCore              0x00000001191e9569 UIApplicationMain + 101
26 SEAnalyticsSDKSample        0x0000000106ec8b05 main + 229
27 dyld                0x0000000107140f21 start_sim + 10
28 ???                 0x000000011063d51e 0x0 + 4569945374
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Frame {{377, 0}, {0, 627}} does not intersect {{377, 0}, {0, 627}}'
terminating with uncaught exception of type NSException
Here's the rough implementation:
func layout() -> UICollectionViewLayout {
    UICollectionViewCompositionalLayout(sectionProvider: { [self] section, _ in
      // Building section here
    }, configuration: configuration)
  }
     
  var configuration: UICollectionViewCompositionalLayoutConfiguration {
    let configuration = UICollectionViewCompositionalLayoutConfiguration()
    var boundaryItems: [NSCollectionLayoutBoundarySupplementaryItem] = []
var offset = 0
for column in 0..<datasource.numberOfColumns {
      let columnHeaderSize = NSCollectionLayoutSize(widthDimension: .absolute(100), heightDimension: .absolute(44))
      let headerView = NSCollectionLayoutBoundarySupplementaryItem(
        layoutSize: columnHeaderSize,
        elementKind: "columnHeader",
        alignment: .topLeading,
        absoluteOffset: CGPoint(x: offset, y: 0)
      )
      headerView.pinToVisibleBounds = true
      headerView.zIndex = 2
       
      offset += 100.0
      boundaryItems.append(headerView)
    }
    configuration.boundarySupplementaryItems = boundaryItems
    return configuration
  }
I'm trying to understand what is causing this crash and how to prevent this crash from happening..

Looks like this crash is happening when there are no items in the collection view and you scroll the global header out of the viewport.
In my case, setting alwaysBounceVertical to false when there are no items and back to true when collection view has items to display right before applying the data from the snapshot to the diffable data source helped to fix the issue:
collectionView.alwaysBounceVertical = !collectionViewItems.isEmpty

Related

The app is crashed when layout 2 text fields horizontally on iOS 16.2

I got the app crashed on the iOS Simulator 16.2 when layout 2 text fields horizontally.
Step by step to reproduce on iOS Simulator 16.2:
Environments:
Xcode 14.2
iOS 16.2
Simulator
Step 1: 2 text fields (UITextField) into a horizontal stackview with leading, trailing, top and bottom constraints.
Step 2: Set a long text for both of 2 text fields (appear three dots at the end of the textfield)
Step 3: click on the first text field
Step 4: click on the second text field
Result: The app is crashed at step 4
Error information:
2023-01-30 14:16:57.647873+0700 iOS16dot2TextFieldBug[18988:1850296] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 274.667]. Layer: <CALayer:0x60000281a600; position = CGPoint (0 0); bounds = CGRect (0 0; 199.667 48); delegate = <_UIEditMenuListView: 0x7f9bac61b960; frame = (-99.8333 -24; 199.667 48); alpha = 0; layer = <CALayer: 0x60000281a600>>; sublayers = (<CALayer: 0x6000028d3740>); opaque = YES; opacity = 0; shadowOpacity = 0.25; shadowOffset = CGSize (0 3); shadowRadius = 12>'
*** First throw call stack:
(
0 CoreFoundation 0x00007ff8004288ab __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007ff80004dba3 objc_exception_throw + 48
2 CoreFoundation 0x00007ff800428789 -[NSException initWithCoder:] + 0
3 QuartzCore 0x00007ff80897d05e -[CALayer setPosition:] + 388
4 UIKitCore 0x000000010a2d90f4 -[UIView setCenter:] + 253
5 UIKitCore 0x0000000109105c2a -[_UIEditMenuPresentation _displayMenu:reason:] + 1959
6 UIKitCore 0x0000000109105387 -[_UIEditMenuPresentation displayMenu:configuration:] + 81
7 UIKitCore 0x000000010a29772a __58-[UIEditMenuInteraction presentEditMenuWithConfiguration:]_block_invoke + 207
8 UIKitCore 0x000000010a299222 __80-[UIEditMenuInteraction _prepareMenuAtLocation:configuration:completionHandler:]_block_invoke + 87
9 UIKitCore 0x000000010a0085cb __133-[_UIContextMenuInteractionBasedTextContextInteraction _editMenuInteraction:menuForConfiguration:suggestedActions:completionHandler:]_block_invoke + 208
10 UIKitCore 0x000000010a008732 -[_UIContextMenuInteractionBasedTextContextInteraction _querySelectionCommandsForConfiguration:suggestedActions:completionHandler:] + 325
11 UIKitCore 0x000000010a0084da -[_UIContextMenuInteractionBasedTextContextInteraction _editMenuInteraction:menuForConfiguration:suggestedActions:completionHandler:] + 126
12 UIKitCore 0x000000010a29918f -[UIEditMenuInteraction _prepareMenuAtLocation:configuration:completionHandler:] + 296
13 UIKitCore 0x000000010a297485 -[UIEditMenuInteraction presentEditMenuWithConfiguration:] + 240
14 UIKitCore 0x000000010a008d6d -[_UIContextMenuInteractionBasedTextContextInteraction presentSelectionCommandsWithConfiguration:] + 94
15 UIKitCore 0x000000010a05cb6f -[UITextSelectionView _presentEditMenuWithPreferredDirection:replacements:] + 103
16 UIKitCore 0x000000010a05c792 -[UITextSelectionView _showCommandsWithReplacements:isForContextMenu:forDictation:arrowDirection:] + 1487
17 UIKitCore 0x000000010a05c157 __77-[UITextSelectionView _showCommandsWithReplacements:forDictation:afterDelay:]_block_invoke + 262
18 libdispatch.dylib 0x000000010246bf5b _dispatch_client_callout + 8
19 libdispatch.dylib 0x000000010246f164 _dispatch_continuation_pop + 836
20 libdispatch.dylib 0x0000000102486d2e _dispatch_source_invoke + 2198
21 libdispatch.dylib 0x000000010247cbc7 _dispatch_main_queue_drain + 1065
22 libdispatch.dylib 0x000000010247c790 _dispatch_main_queue_callback_4CF + 31
23 CoreFoundation 0x00007ff800387b1f __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
24 CoreFoundation 0x00007ff800382436 __CFRunLoopRun + 2482
25 CoreFoundation 0x00007ff8003816a7 CFRunLoopRunSpecific + 560
26 GraphicsServices 0x00007ff809cb128a GSEventRunModal + 139
27 UIKitCore 0x0000000109ccbad3 -[UIApplication _run] + 994
28 UIKitCore 0x0000000109cd09ef UIApplicationMain + 123
29 libswiftUIKit.dylib 0x0000000102a18c72 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
30 iOS16dot2TextFieldBug 0x000000010223c908 $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 104
31 iOS16dot2TextFieldBug 0x000000010223c897 $s21iOS16dot2TextFieldBug11AppDelegateC5$mainyyFZ + 39
32 iOS16dot2TextFieldBug 0x000000010223c988 main + 24
33 dyld 0x00000001025912bf start_sim + 10
34 ??? 0x0000000102375310 0x0 + 4332147472
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 274.667]. Layer: <CALayer:0x60000281a600; position = CGPoint (0 0); bounds = CGRect (0 0; 199.667 48); delegate = <_UIEditMenuListView: 0x7f9bac61b960; frame = (-99.8333 -24; 199.667 48); alpha = 0; layer = <CALayer: 0x60000281a600>>; sublayers = (<CALayer: 0x6000028d3740>); opaque = YES; opacity = 0; shadowOpacity = 0.25; shadowOffset = CGSize (0 3); shadowRadius = 12>'
terminating with uncaught exception of type NSException
CoreSimulator 857.14 - Device: iPhone 14 (BA2F723B-EEE6-48D0-89DD-9DAEC9E9A11E) - Runtime: iOS 16.2 (20C52) - DeviceType: iPhone 14
I just got this issue on iOS Simulator 16.2. Everything is normal on the lower than iOS 16.2.
Full of code here
I don't know this is an issue from iOS 16.2 or not.
Have you seen this issue? Please help me fix it if you can.
Thank you.
It certainly appears to be a bug.
One work-around is to add this in viewDidLoad() (or set in Storyboard):
[emailTextField, websiteTextField].forEach {
if let tf = $0 {
tf.autocorrectionType = .no
tf.spellCheckingType = .no
}
}
Your example is looking for "email" and "website" entries, so you probably don't want/need those anyway.

Application Crash in tableview with CALayer position contains NaN: [duplicate]

I got the app crashed on the iOS Simulator 16.2 when layout 2 text fields horizontally.
Step by step to reproduce on iOS Simulator 16.2:
Environments:
Xcode 14.2
iOS 16.2
Simulator
Step 1: 2 text fields (UITextField) into a horizontal stackview with leading, trailing, top and bottom constraints.
Step 2: Set a long text for both of 2 text fields (appear three dots at the end of the textfield)
Step 3: click on the first text field
Step 4: click on the second text field
Result: The app is crashed at step 4
Error information:
2023-01-30 14:16:57.647873+0700 iOS16dot2TextFieldBug[18988:1850296] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 274.667]. Layer: <CALayer:0x60000281a600; position = CGPoint (0 0); bounds = CGRect (0 0; 199.667 48); delegate = <_UIEditMenuListView: 0x7f9bac61b960; frame = (-99.8333 -24; 199.667 48); alpha = 0; layer = <CALayer: 0x60000281a600>>; sublayers = (<CALayer: 0x6000028d3740>); opaque = YES; opacity = 0; shadowOpacity = 0.25; shadowOffset = CGSize (0 3); shadowRadius = 12>'
*** First throw call stack:
(
0 CoreFoundation 0x00007ff8004288ab __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007ff80004dba3 objc_exception_throw + 48
2 CoreFoundation 0x00007ff800428789 -[NSException initWithCoder:] + 0
3 QuartzCore 0x00007ff80897d05e -[CALayer setPosition:] + 388
4 UIKitCore 0x000000010a2d90f4 -[UIView setCenter:] + 253
5 UIKitCore 0x0000000109105c2a -[_UIEditMenuPresentation _displayMenu:reason:] + 1959
6 UIKitCore 0x0000000109105387 -[_UIEditMenuPresentation displayMenu:configuration:] + 81
7 UIKitCore 0x000000010a29772a __58-[UIEditMenuInteraction presentEditMenuWithConfiguration:]_block_invoke + 207
8 UIKitCore 0x000000010a299222 __80-[UIEditMenuInteraction _prepareMenuAtLocation:configuration:completionHandler:]_block_invoke + 87
9 UIKitCore 0x000000010a0085cb __133-[_UIContextMenuInteractionBasedTextContextInteraction _editMenuInteraction:menuForConfiguration:suggestedActions:completionHandler:]_block_invoke + 208
10 UIKitCore 0x000000010a008732 -[_UIContextMenuInteractionBasedTextContextInteraction _querySelectionCommandsForConfiguration:suggestedActions:completionHandler:] + 325
11 UIKitCore 0x000000010a0084da -[_UIContextMenuInteractionBasedTextContextInteraction _editMenuInteraction:menuForConfiguration:suggestedActions:completionHandler:] + 126
12 UIKitCore 0x000000010a29918f -[UIEditMenuInteraction _prepareMenuAtLocation:configuration:completionHandler:] + 296
13 UIKitCore 0x000000010a297485 -[UIEditMenuInteraction presentEditMenuWithConfiguration:] + 240
14 UIKitCore 0x000000010a008d6d -[_UIContextMenuInteractionBasedTextContextInteraction presentSelectionCommandsWithConfiguration:] + 94
15 UIKitCore 0x000000010a05cb6f -[UITextSelectionView _presentEditMenuWithPreferredDirection:replacements:] + 103
16 UIKitCore 0x000000010a05c792 -[UITextSelectionView _showCommandsWithReplacements:isForContextMenu:forDictation:arrowDirection:] + 1487
17 UIKitCore 0x000000010a05c157 __77-[UITextSelectionView _showCommandsWithReplacements:forDictation:afterDelay:]_block_invoke + 262
18 libdispatch.dylib 0x000000010246bf5b _dispatch_client_callout + 8
19 libdispatch.dylib 0x000000010246f164 _dispatch_continuation_pop + 836
20 libdispatch.dylib 0x0000000102486d2e _dispatch_source_invoke + 2198
21 libdispatch.dylib 0x000000010247cbc7 _dispatch_main_queue_drain + 1065
22 libdispatch.dylib 0x000000010247c790 _dispatch_main_queue_callback_4CF + 31
23 CoreFoundation 0x00007ff800387b1f __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
24 CoreFoundation 0x00007ff800382436 __CFRunLoopRun + 2482
25 CoreFoundation 0x00007ff8003816a7 CFRunLoopRunSpecific + 560
26 GraphicsServices 0x00007ff809cb128a GSEventRunModal + 139
27 UIKitCore 0x0000000109ccbad3 -[UIApplication _run] + 994
28 UIKitCore 0x0000000109cd09ef UIApplicationMain + 123
29 libswiftUIKit.dylib 0x0000000102a18c72 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
30 iOS16dot2TextFieldBug 0x000000010223c908 $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 104
31 iOS16dot2TextFieldBug 0x000000010223c897 $s21iOS16dot2TextFieldBug11AppDelegateC5$mainyyFZ + 39
32 iOS16dot2TextFieldBug 0x000000010223c988 main + 24
33 dyld 0x00000001025912bf start_sim + 10
34 ??? 0x0000000102375310 0x0 + 4332147472
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 274.667]. Layer: <CALayer:0x60000281a600; position = CGPoint (0 0); bounds = CGRect (0 0; 199.667 48); delegate = <_UIEditMenuListView: 0x7f9bac61b960; frame = (-99.8333 -24; 199.667 48); alpha = 0; layer = <CALayer: 0x60000281a600>>; sublayers = (<CALayer: 0x6000028d3740>); opaque = YES; opacity = 0; shadowOpacity = 0.25; shadowOffset = CGSize (0 3); shadowRadius = 12>'
terminating with uncaught exception of type NSException
CoreSimulator 857.14 - Device: iPhone 14 (BA2F723B-EEE6-48D0-89DD-9DAEC9E9A11E) - Runtime: iOS 16.2 (20C52) - DeviceType: iPhone 14
I just got this issue on iOS Simulator 16.2. Everything is normal on the lower than iOS 16.2.
Full of code here
I don't know this is an issue from iOS 16.2 or not.
Have you seen this issue? Please help me fix it if you can.
Thank you.
It certainly appears to be a bug.
One work-around is to add this in viewDidLoad() (or set in Storyboard):
[emailTextField, websiteTextField].forEach {
if let tf = $0 {
tf.autocorrectionType = .no
tf.spellCheckingType = .no
}
}
Your example is looking for "email" and "website" entries, so you probably don't want/need those anyway.

Simple TabView is crashing in iOS 16

It's fixed at iOS 16.1 Beta 4 ✅
There is a weird crash that only happens in iOS 16. When I try to create a TabView with page style, it's crashing when the array is empty. But it's working perfectly fine in iOS 15. Is this an iOS 16 bug?
Xcode version: Version 14.0 (14A309)
iOS Version: 16.0 & 16.1 Beta
struct TestView: View {
#State private var data: [Int] = []
var body: some View {
TabView {
ForEach(0..<data.count, id: \.self) { index in
RoundedRectangle(cornerRadius: 12)
}
}
.frame(height: 200)
.tabViewStyle(.page(indexDisplayMode: .never))
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.data = [1,2,3,4]
}
}
}
}
Crash Log
2022-10-04 18:01:56.275879+0300 TestFirebaseCrash[24331:486169] *** Assertion failure in -[_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF020PagingCollectionView _validateScrollingTargetIndexPath:], UICollectionView.m:7339
2022-10-04 18:01:56.278900+0300 TestFirebaseCrash[24331:486169] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to scroll the collection view to an out-of-bounds item (0) when there are only 0 items in section 0. Collection view: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF020PagingCollectionView: 0x140841e00; baseClass = UICollectionView; frame = (0 0; 428 200); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001d8d260>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x600001386d20>; contentOffset: {0, 0}; contentSize: {0, 200}; adjustedContentInset: {0, 0, 0, 0}; layout: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF012PagingLayout: 0x140514090>; dataSource: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF011Coordinator: 0x6000021f46c0>>.'
*** First throw call stack:
(
0 CoreFoundation 0x000000018040c304 __exceptionPreprocess + 172
1 libobjc.A.dylib 0x000000018005131c objc_exception_throw + 56
2 Foundation 0x0000000180b11f88 _userInfoForFileAndLine + 0
3 UIKitCore 0x00000001032279a8 -[UICollectionView _validateScrollingTargetIndexPath:] + 408
4 UIKitCore 0x0000000103227cc0 -[UICollectionView _contentOffsetForScrollingToItemAtIndexPath:atScrollPosition:] + 48
5 UIKitCore 0x0000000103228838 -[UICollectionView _scrollToItemAtIndexPath:atScrollPosition:animated:] + 220
6 SwiftUI 0x0000000106f5de08 OUTLINED_FUNCTION_15 + 18076
7 SwiftUI 0x0000000106f5dea8 OUTLINED_FUNCTION_15 + 18236
8 SwiftUI 0x0000000106f5dc88 OUTLINED_FUNCTION_15 + 17692
9 SwiftUI 0x0000000106f5dcf4 OUTLINED_FUNCTION_15 + 17800
10 UIKitCore 0x0000000103ea214c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1920
11 QuartzCore 0x0000000187ed749c _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 440
12 QuartzCore 0x0000000187ee1f54 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 128
13 QuartzCore 0x0000000187e117d0 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 452
14 QuartzCore 0x0000000187e3d47c _ZN2CA11Transaction6commitEv + 652
15 UIKitCore 0x0000000103a257f0 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32
16 CoreFoundation 0x0000000180370e38 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
17 CoreFoundation 0x0000000180370580 __CFRunLoopDoBlocks + 364
18 CoreFoundation 0x000000018036ae7c __CFRunLoopRun + 776
19 CoreFoundation 0x000000018036a75c CFRunLoopRunSpecific + 584
20 GraphicsServices 0x0000000188f60c98 GSEventRunModal + 160
21 UIKitCore 0x0000000103a0cb74 -[UIApplication _run] + 868
22 UIKitCore 0x0000000103a10b1c UIApplicationMain + 124
23 SwiftUI 0x0000000107702130 OUTLINED_FUNCTION_51 + 496
24 SwiftUI 0x0000000107701fd8 OUTLINED_FUNCTION_51 + 152
25 SwiftUI 0x0000000106ee5894 OUTLINED_FUNCTION_10 + 88
26 TestFirebaseCrash 0x000000010269ec6c $s17TestFirebaseCrash0abC3AppV5$mainyyFZ + 40
27 TestFirebaseCrash 0x000000010269ed14 main + 12
28 dyld 0x00000001028e9fa0 start_sim + 20
29 ??? 0x00000001029bd08c 0x0 + 4338733196
30 ??? 0x1a00000000000000 0x0 + 1873497444986126336
)
libc++abi: terminating with uncaught exception of type NSException
terminating with uncaught exception of type NSException
CoreSimulator 857.10 - Device: iPhone 13 Pro Max (29ECC9E3-B223-4591-A18A-1C858F528C45) - Runtime: iOS 16.0 (20A360) - DeviceType: iPhone 13 Pro Max
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to scroll the collection view to an out-of-bounds item (0) when there are only 0 items in section 0. Collection view: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF020PagingCollectionView: 0x140841e00; baseClass = UICollectionView; frame = (0 0; 428 200); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001d8d260>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x600001386d20>; contentOffset: {0, 0}; contentSize: {0, 200}; adjustedContentInset: {0, 0, 0, 0}; layout: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF012PagingLayout: 0x140514090>; dataSource: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF011Coordinator: 0x6000021f46c0>>.'
(lldb)
Try returning Text("") (basically an empty view) if data is empty.
TabView {
if data.isEmpty {
Text("")
} else {
ForEach(data, id: \.self) { _ in
RoundedRectangle(cornerRadius: 12)
}
}
}
It looks like it's fixed at iOS 16.1 Beta 4 ✅

SwiftUI app crash with "attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update" error

My app started crashing randomly during launch with below error. Sometimes it starts and sometimes it crashes. How can I investigate this bug? Any help appreciated
2021-06-21 09:44:44.940545+0200 APP[18897:3547429] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update'
*** First throw call stack:
(
0 CoreFoundation 0x00000001186ebfba __exceptionPreprocess + 242
1 libobjc.A.dylib 0x0000000118595ff5 objc_exception_throw + 48
2 CoreFoundation 0x00000001186ebde3 +[NSException raise:format:] + 0
3 Foundation 0x000000010ffa88e7 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 UIKitCore 0x00000001245098a3 -[UITableView _endCellAnimationsWithContext:] + 9269
5 UIKitCore 0x0000000124524ff7 -[UITableView endUpdatesWithContext:] + 116
6 UIKitCore 0x00000001245251c9 -[UITableView _performBatchUpdates:withContext:completion:] + 253
7 UIKitCore 0x00000001245252af -[UITableView performBatchUpdates:completion:] + 97
8 SwiftUI 0x000000011527a801 $s7SwiftUI25UpdateCoalescingTableView33_BFB370BA5F1BADDC9D83021565761A49LLC19performBatchUpdates_10completionyyycSg_ySbcSgtF + 378
9 SwiftUI 0x000000011527a8da $s7SwiftUI25UpdateCoalescingTableView33_BFB370BA5F1BADDC9D83021565761A49LLC19performBatchUpdates_10completionyyycSg_ySbcSgtFTo + 175
10 SwiftUI 0x000000011527d31d $s7SwiftUI19ListCoreCoordinator33_BFB370BA5F1BADDC9D83021565761A49LLC17updateUITableView_2to11transactionySo0mN0C_xAA11TransactionVtF14performUpdatesL_yyAA0cD10DataSourceRzAA16SelectionManagerR_0V5ValueQy_AORtzr0_lF + 1594
11 SwiftUI 0x0000000115623652 $sIg_Ieg_TR + 12
12 SwiftUI 0x0000000115623668 $sIeg_IyB_TR + 14
13 UIKitCore 0x0000000124856bb9 +[UIView(Animation) performWithoutAnimation:] + 84
14 SwiftUI 0x000000011527dc6f $s7SwiftUI19ListCoreCoordinator33_BFB370BA5F1BADDC9D83021565761A49LLC17updateUITableView_2to11transactionySo0mN0C_xAA11TransactionVtFyycfU_ + 467
15 SwiftUI 0x0000000115166eb7 $sIeg_ytIegr_TR + 12
16 SwiftUI 0x00000001152851cf $sIeg_ytIegr_TRTA + 17
17 SwiftUI 0x000000011528899f $sIeg_ytIegr_TRTA.42 + 9
18 SwiftUI 0x0000000115150ec4 $s7SwiftUI6UpdateO3endyyFZ + 433
19 SwiftUI 0x0000000114fa83fd $sSo9NSRunLoopC7SwiftUIE14flushObserversyyFZ + 148
20 SwiftUI 0x0000000114fa8367 $sSo9NSRunLoopC7SwiftUIE11addObserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_yyXEfU_ + 9
21 SwiftUI 0x0000000114fa35fe $ss5Error_pIgzo_ytsAA_pIegrzo_TR95$sSo9NSRunLoopC7SwiftUIE11addObserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_yyXEfU_Tf3npf_n + 14
22 libswiftObjectiveC.dylib 0x0000000119a9603e $s10ObjectiveC15autoreleasepool8invokingxxyKXE_tKlF + 46
23 SwiftUI 0x0000000114fa8356 $sSo9NSRunLoopC7SwiftUIE11addObserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_ + 35
24 SwiftUI 0x0000000114fa8459 $sSo9NSRunLoopC7SwiftUIE11addObserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_To + 43
25 CoreFoundation 0x0000000118658d31 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
26 CoreFoundation 0x0000000118653542 __CFRunLoopDoObservers + 541
27 CoreFoundation 0x0000000118653af5 __CFRunLoopRun + 1129
28 CoreFoundation 0x00000001186531a7 CFRunLoopRunSpecific + 567
29 GraphicsServices 0x000000011c844d85 GSEventRunModal + 139
30 UIKitCore 0x000000012430c4df -[UIApplication _run] + 912
31 UIKitCore 0x000000012431139c UIApplicationMain + 101
32 1A-Client 0x000000010485fb5b main + 75
33 libdyld.dylib 0x0000000119e1cbbd start + 1
34 ??? 0x0000000000000001 0x0 + 1
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update'
terminating with uncaught exception of type NSException
CoreSimulator 772.1 - Device: iPhone 12 (6C4D1499-EBAC-4ED6-9C38-7DB0EB0C63E9) - Runtime: iOS 14.5 (18E182) - DeviceType: iPhone 12
I've fixed this crash by forcing section rewrite on every view refresh. It looks not optimal but the problems seems to be problem with SwiftUI view update cycle
Section {
content()
}
.id(UUID())

UISearchController crash on iOS13

Working on iOS12 and prior, but crashes on iOS13 with this error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UISearchController's control over its search bar has been destroyed. Please use the search bar provided by the UISearchController. searchController = <AdvancedSearchController: 0x7fbd9d224a00>, searchBar = <TDSearchBar: 0x7fbd9b768480; baseClass = UISearchBar; frame = (0 0; 1024 50); text = ''; gestureRecognizers = <NSArray: 0x600002791770>; layer = <CALayer: 0x600002ef9be0>>'
How con i solve? What does this exception mean?
*** First throw call stack:
(
0 CoreFoundation 0x000000010e296c3e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x000000010e104de0 objc_exception_throw + 48
2 CoreFoundation 0x000000010e296a7c +[NSException raise:format:] + 188
3 UIKitCore 0x000000011fa6e6b0 -[UISearchController _animatorForBarPresentationStyle:dismissing:] + 185
4 UIKitCore 0x000000011fa6eb24 -[UISearchController animationControllerForPresentedController:presentingController:sourceController:] + 34
5 UIKitCore 0x000000011f3a03a0 -[UIViewController _customAnimatorForPresentedController:presentingController:sourceController:] + 154
6 UIKitCore 0x000000011f3a4534 -[UIViewController _presentViewController:withAnimationController:completion:] + 4472
7 UIKitCore 0x000000011f3a6d1b __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 98
8 UIKitCore 0x000000011f3a7233 -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 511
9 UIKitCore 0x000000011f3a6c79 -[UIViewController _presentViewController:animated:completion:] + 187
10 UIKitCore 0x000000011f3a6ee0 -[UIViewController presentViewController:animated:completion:] + 150
Solved. You cannot neither subclass UISearchBar in UISearchController...at least in beta 3.

Resources