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!