Floating NSInternalInconsistencyException crash when printing PDF

I use the following code to print PDF:

let printInfo = UIPrintInfo.printInfo()
printInfo.outputType = UIPrintInfo.OutputType.general
printInfo.jobName = "My Print Job"
printInfo.orientation = .portrait

let printController = UIPrintInteractionController.shared
printController.printInfo = printInfo
printController.printingItem = pdfData
printController.delegate = self
printController.present(animated: true, completionHandler: nil)

Everything works fine, but according to the Xcode Organizer, some users experience sporadic crashes after initiating printing. It appears that these crashes may be related to updates in the UI within UIPrintInteractionController. The majority of these crashes occur on iOS 16, with a few also affecting iOS 17. I am unable to reproduce the issue.

Stack trace for iOS 16.6.1:

Fatal Exception: NSInternalInconsistencyException
Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (0), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). Collection view: <UICollectionView: 0x14d9a1a00; frame = (0 0; 235 944); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x281fe9fb0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x28164e660>; contentOffset: {0, 0}; contentSize: {235, 88}; adjustedContentInset: {0, 0, 0, 0}; layout: <UIPrintPreviewFlowLayout: 0x14b7c1320>; dataSource: <UIPrintPreviewViewController: 0x14d811200>>

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x9cb4 __exceptionPreprocess
1  libobjc.A.dylib                0x183d0 objc_exception_throw
2  Foundation                     0x4e154c _userInfoForFileAndLine
3  UIKitCore                      0x7024f4 -[UICollectionView _Bug_Detected_In_Client_Of_UICollectionView_Invalid_Number_Of_Items_In_Section:]
4  UIKitCore                      0x30d5a8 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:collectionViewAnimator:]
5  UIKitCore                      0x26bd5c -[UICollectionView _updateRowsAtIndexPaths:updateAction:updates:]
6  UIKitCore                      0x26d0e0 -[UICollectionView reloadItemsAtIndexPaths:]
7  PrintKitUI                     0x2677c __55-[UIPrintPreviewViewController updatePrintPreviewInfo:]_block_invoke_3
8  UIKitCore                      0x30494 +[UIView(Animation) performWithoutAnimation:]
9  PrintKitUI                     0x266c8 __55-[UIPrintPreviewViewController updatePrintPreviewInfo:]_block_invoke
10 PrintKitUI                     0x57b30 -[UIPrintPreviewPageFetcher resetAllPages:]
11 PrintKitUI                     0x26460 -[UIPrintPreviewViewController updatePrintPreviewInfo:]
12 PrintKitUI                     0x47b84 -[UIPrintPanelViewController updatePrintPreviewInfo]
13 PrintKitUI                     0x45d5c -[UIPrintPanelViewController observeValueForKeyPath:ofObject:change:context:]
14 Foundation                     0x3c0d0 NSKeyValueNotifyObserver
15 Foundation                     0x52618 NSKeyValueDidChange
16 Foundation                     0x3f518 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:]
17 Foundation                     0x3f248 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:]
18 Foundation                     0x3e1f8 _NSSetLongLongValueAndNotify
19 PrintKitUI                     0x52e00 -[UIPrintInColorOption observeValueForKeyPath:ofObject:change:context:]
20 Foundation                     0x3c0d0 NSKeyValueNotifyObserver
21 Foundation                     0x52618 NSKeyValueDidChange
22 Foundation                     0x3f518 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:]
23 Foundation                     0x3f248 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:]
24 Foundation                     0x3e7dc _NSSetObjectValueAndNotify
25 PrintKitUI                     0x496f0 __51-[UIPrintPanelViewController lookupLastUsedPrinter]_block_invoke_2
26 libdispatch.dylib              0x2320 _dispatch_call_block_and_release
27 libdispatch.dylib              0x3eac _dispatch_client_callout
...

Stack trace for iOS 17.0.3:

Fatal Exception: NSInternalInconsistencyException
UITableView dataSource returned a nil cell for row at index path: <NSIndexPath: 0x96b9dd5bd6ad637d> {length = 2, path = 0 - 0}. Table view: <UITableView: 0x108846400; frame = (0 0; 430 542); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x281fa8ae0>; backgroundColor = <UIDynamicSystemColor: 0x280481bc0; name = systemGroupedBackgroundColor>; layer = <CALayer: 0x2817cbea0>; contentOffset: {0, -56}; contentSize: {430, 332.99999618530273}; adjustedContentInset: {56, 0, 0, 0}; dataSource: <UIPrintOptionsTableViewController: 0x108819600>>, dataSource: <UIPrintOptionsTableViewController: 0x108819600>

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0xed5e0 __exceptionPreprocess
1  libobjc.A.dylib                0x2bc00 objc_exception_throw
2  Foundation                     0x6bc398 _userInfoForFileAndLine
3  UIKitCore                      0x289d74 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
4  UIKitCore                      0x289a70 -[UITableView _updateVisibleCellsForRanges:createIfNecessary:]
5  UIKitCore                      0x287fbc -[UITableView _updateVisibleCellsNow:]
6  UIKitCore                      0xfc45dc -[UITableView _updateAnimationDidStopWithOldVisibleViews:finished:context:]
7  UIKitCore                      0x850bc __UIVIEW_IS_EXECUTING_ANIMATION_COMPLETION_BLOCK__
8  UIKitCore                      0x84854 -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:]
9  UIKitCore                      0x83ecc -[UIViewAnimationState sendDelegateAnimationDidStop:finished:]
10 UIKitCore                      0x66498 -[UIViewAnimationState animationDidStop:finished:]
11 QuartzCore                     0x7276c run_animation_callbacks(void*)
12 libdispatch.dylib              0x4300 _dispatch_client_callout
...

Is anyone experiencing similar issues?

Thanks!

I have a consistent crash while trying to share the PDF from UIPrintInteractionController since the latest OS update with NSInternalInconsistencyException . Happens only from iOS 17 onwards, let me know if you've found a solution!

We also have Crashes (with variations) with UIActivityViewController. The problem is, that we can reproduce it, but don't know how to fix it. Maybe it's a problem of share sheet. The other problem is, that the crash varies depending on the device and iOS version. We can reproduce a variation with iOS Simulator "iPad Pro (9.7-inch) iOS 16.4".

  • PDF share with UIActivityViewController
  • print
  • Layout -> change pages per sheet multiple times (fast) -> app crashes

Let me know if you've found a solution.

*** Assertion failure in -[UICollectionView _Bug_Detected_In_Client_Of_UICollectionView_Invalid_Number_Of_Items_In_Section:], UICollectionView.m:10074
** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (5) must be equal to the number of items contained in that section before the update (3), plus or minus the number of items inserted or deleted from that section (3 inserted, 3 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). Collection view: <UICollectionView: 0x14196ea00; frame = (0 56; 235 888); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x6000000f08d0>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x600000a32200>; contentOffset: {0, 0}; contentSize: {235, 968}; adjustedContentInset: {0, 0, 0, 0}; layout: <UIPrintPreviewFlowLayout: 0x2ad32d040>; dataSource: <UIPrintPreviewViewController: 0x14196aa00>>'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000106c1a330 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x00000001059bd274 objc_exception_throw + 56
	2   Foundation                          0x0000000107cb89b4 _userInfoForFileAndLine + 0
	3   UIKitCore                           0x000000011a59a24c -[UICollectionView _Bug_Detected_In_Client_Of_UICollectionView_Invalid_Number_Of_Items_In_Section:] + 92
	4   UIKitCore                           0x000000011a599cfc -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:collectionViewAnimator:] + 10048
	5   UIKitCore                           0x000000011a596a3c -[UICollectionView _updateRowsAtIndexPaths:updateAction:updates:] + 360
	6   UIKitCore                           0x000000011a596b70 -[UICollectionView reloadItemsAtIndexPaths:] + 48
	7   UIKit                               0x00000001473efcd8 -[UICollectionViewAccessibility reloadItemsAtIndexPaths:] + 48
	8   PrintKitUI                          0x000000010c97a2d4 __55-[UIPrintPreviewViewController updatePrintPreviewInfo:]_block_invoke_3 + 76
	9   UIKitCore                           0x000000011b228138 +[UIView(Animation) performWithoutAnimation:] + 68
	10  PrintKitUI                          0x000000010c97a238 __55-[UIPrintPreviewViewController updatePrintPreviewInfo:]_block_invoke + 520
	11  PrintKitUI                          0x000000010c9a7b68 -[UIPrintPreviewPageFetcher resetAllPages:] + 48
	12  PrintKitUI                          0x000000010c97a00c -[UIPrintPreviewViewController updatePrintPreviewInfo:] + 116
	13  PrintKitUI                          0x000000010c998f48 -[UIPrintPanelViewController updatePrintPreviewInfo] + 68
	14  PrintKitUI                          0x000000010c9971f4 -[UIPrintPanelViewController observeValueForKeyPath:ofObject:change:context:] + 312
	15  Foundation                          0x0000000107d04f1c NSKeyValueNotifyObserver + 248
	16  Foundation                          0x0000000107d07e98 NSKeyValueDidChange + 352
	17  Foundation                          0x0000000107d0786c -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:] + 644
	18  Foundation                          0x0000000107d08064 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:] + 60
	19  Foundation                          0x0000000107d0168c _NSSetObjectValueAndNotify + 256
	20  PrintKitUI                          0x000000010c989c14 -[UIPrintPagesPerSheetOption listItemSelected:] + 276
	21  PrintKitUI                          0x000000010c96ce0c -[UIPrintOptionListViewController tableView:didSelectRowAtIndexPath:] + 112
	22  UIKitCore                           0x000000011af622b8 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:isCellMultiSelect:deselectPrevious:performCustomSelectionAction:] + 1208
	23  UIKitCore                           0x000000011af625c8 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 252
	24  UIKitCore                           0x000000011b1dd9e8 -[_UIAfterCACommitBlock run] + 64
	25  UIKitCore                           0x000000011b1dde14 -[_UIAfterCACommitQueue flush] + 164
	26  UIKitCore                           0x000000011ad9bce8 _runAfterCACommitDeferredBlocks + 492
	27  UIKitCore                           0x000000011ad8da6c _cleanUpAfterCAFlushAndRunDeferredBlocks + 104
	28  UIKitCore                           0x000000011adb7d50 _afterCACommitHandler + 60
	29  CoreFoundation                      0x0000000106b7cc10 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
	30  CoreFoundation                      0x0000000106b7757c __CFRunLoopDoObservers + 512
	31  CoreFoundation                      0x0000000106b77a20 __CFRunLoopRun + 948
	32  CoreFoundation                      0x0000000106b77254 CFRunLoopRunSpecific + 584
	33  GraphicsServices                    0x000000010d11fc9c GSEventRunModal + 160
	34  UIKitCore                           0x000000011ad8eff0 -[UIApplication _run] + 868
	35  UIKitCore                           0x000000011ad92f3c UIApplicationMain + 124
Floating NSInternalInconsistencyException crash when printing PDF
 
 
Q