Reputation: 21
I am encountering an error when attempting to cache decorations in a UICalendarView. The code to reproduce the error is as follows:
class Coordinator: NSObject, UICalendarViewDelegate, UICalendarSelectionSingleDateDelegate {
// ...
private var decorationCache: [DateComponents: UICalendarView.Decoration?] = [:]
func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Decoration? {
if let decoration = decorationCache[dateComponents] {
return decoration
}
let decoration = calculateDecoration(dateComponents: dateComponents)
decorationCache[dateComponents] = decoration
return decoration
}
// ...
}
The error that occurs is as follows:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
Even if I change the line let decoration = calculateDecoration(dateComponents: dateComponents)
to let decoration = UICalendarView.Decoration.default()
the same error occurs.
What could be causing this issue?
stack trace ↓
* thread #1, queue = 'com.apple.uikit.datasource.diffing', stop reason = breakpoint 1.1
* frame #0: 0x0000000100abf6e4 JobShift`CalendarView.Coordinator.calendarView(calendarView=0x00000001212052d0, dateComponents=Foundation.DateComponents @ 0x000000016f3d7850, self=0x0000000121204fe0) at CalendarView.swift:47:24
frame #1: 0x0000000100abf880 JobShift`@objc CalendarView.Coordinator.calendarView(_:decorationFor:) at <compiler-generated>:0
frame #2: 0x00000001aa753d60 UIKitCore`-[UICalendarView _delegateDecorationForDay:] + 108
frame #3: 0x00000001aa753bdc UIKitCore`-[UICalendarView _configuedCellForCollectionView:indexPath:day:] + 172
frame #4: 0x00000001aa752708 UIKitCore`__37-[UICalendarView _setupViewHierarchy]_block_invoke + 96
frame #5: 0x00000001a9f72854 UIKitCore`-[__UIDiffableDataSource collectionView:cellForItemAtIndexPath:] + 144
frame #6: 0x00000001a9f72598 UIKitCore`-[UICollectionViewDiffableDataSource collectionView:cellForItemAtIndexPath:] + 76
frame #7: 0x00000001a9f6fc1c UIKitCore`-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 1000
frame #8: 0x00000001a9f6f648 UIKitCore`-[UICollectionView _createVisibleViewsForSingleCategoryAttributes:limitCreation:fadeForBoundsChange:] + 1116
frame #9: 0x00000001aa0a7330 UIKitCore`-[UICollectionView _createVisibleViewsForAttributes:fadeForBoundsChange:notifyLayoutForVisibleCellsPass:] + 300
frame #10: 0x00000001a9e98120 UIKitCore`-[UICollectionView _updateVisibleCellsNow:] + 3060
frame #11: 0x00000001aa003a2c UIKitCore`-[UICollectionView layoutSubviews] + 304
frame #12: 0x00000001a9cdea4c UIKitCore`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1528
frame #13: 0x00000001a913d3b4 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 504
frame #14: 0x00000001a9d27e90 UIKitCore`-[UIView(Hierarchy) layoutBelowIfNeeded] + 296
frame #15: 0x00000001a9f86e1c UIKitCore`-[UICollectionView reloadData] + 2272
frame #16: 0x00000001a9dc46d4 UIKitCore`-[UICollectionView _performInternalBatchUpdates:] + 56
frame #17: 0x00000001a9dc453c UIKitCore`-[UICollectionView _performDiffableUpdate:] + 88
frame #18: 0x00000001aa2279a4 UIKitCore`-[_UIDiffableDataSourceViewUpdater _reloadData] + 144
frame #19: 0x00000001aa2278a4 UIKitCore`__56-[__UIDiffableDataSource reloadFromSnapshot:completion:]_block_invoke + 164
frame #20: 0x00000001aa227774 UIKitCore`__56-[__UIDiffableDataSource reloadFromSnapshot:completion:]_block_invoke_2 + 52
frame #21: 0x0000000106e5a7bc libdispatch.dylib`_dispatch_client_callout + 20
frame #22: 0x0000000106e6be24 libdispatch.dylib`_dispatch_lane_barrier_sync_invoke_and_complete + 176
frame #23: 0x00000001aa09d044 UIKitCore`-[__UIDiffableDataSource reloadFromSnapshot:completion:] + 556
frame #24: 0x00000001a9f779dc UIKitCore`-[UICollectionViewDiffableDataSource applySnapshotUsingReloadData:completion:] + 72
frame #25: 0x00000001aa7b0a1c UIKitCore`-[_UICalendarViewDataSourceController _reloadDataSourceWithScrollPosition:] + 340
frame #26: 0x00000001aa7b0ab4 UIKitCore`-[_UICalendarViewDataSourceController cleanupDataSourceWithScrollPositionIfNecessary:] + 84
frame #27: 0x00000001aa756bcc UIKitCore`__41-[UICalendarView _cleanupDataIfNecessary]_block_invoke + 84
frame #28: 0x00000001aa756790 UIKitCore`-[UICalendarView _performIgnoringScrollCallbacks:] + 76
frame #29: 0x00000001aa756b5c UIKitCore`-[UICalendarView _cleanupDataIfNecessary] + 104
frame #30: 0x00000001aa756e24 UIKitCore`-[UICalendarView scrollViewDidEndDecelerating:] + 96
frame #31: 0x00000001aaf35d50 UIKitCore`-[UIScrollView _scrollViewDidEndDeceleratingForDelegate] + 200
frame #32: 0x00000001aaf344f4 UIKitCore`-[UIScrollView _stopScrollDecelerationNotify:] + 756
frame #33: 0x00000001a9f73348 UIKitCore`-[UICollectionView _stopScrollingNotify:pin:] + 52
frame #34: 0x00000001aaf30b0c UIKitCore`-[UIScrollView _smoothScrollSyncWithUpdateTime:] + 2172
frame #35: 0x00000001aaf2ff24 UIKitCore`-[UIScrollView _smoothScrollWithUpdateTime:] + 124
frame #36: 0x00000001aaf30d78 UIKitCore`-[UIScrollView _smoothScrollDisplayLink:] + 416
frame #37: 0x00000001a918fecc QuartzCore`CA::Display::DisplayLinkItem::dispatch_(CA::SignPost::Interval<(CA::SignPost::CAEventCode)835322056>&) + 48
frame #38: 0x00000001a918e230 QuartzCore`CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 876
frame #39: 0x00000001a918ddd0 QuartzCore`CA::Display::DisplayLink::dispatch_deferred_display_links(unsigned int) + 352
frame #40: 0x00000001a9d8576c UIKitCore`_UIUpdateSequenceRun + 84
frame #41: 0x00000001a9d853b0 UIKitCore`schedulerStepScheduledMainSection + 172
frame #42: 0x00000001a9d86254 UIKitCore`runloopSourceCallback + 92
frame #43: 0x00000001a7aa3834 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
frame #44: 0x00000001a7aa37c8 CoreFoundation`__CFRunLoopDoSource0 + 176
frame #45: 0x00000001a7aa1298 CoreFoundation`__CFRunLoopDoSources0 + 244
frame #46: 0x00000001a7aa0484 CoreFoundation`__CFRunLoopRun + 828
frame #47: 0x00000001a7a9fcd8 CoreFoundation`CFRunLoopRunSpecific + 608
frame #48: 0x00000001ec9501a8 GraphicsServices`GSEventRunModal + 164
frame #49: 0x00000001aa0d890c UIKitCore`-[UIApplication _run] + 888
frame #50: 0x00000001aa18c9d0 UIKitCore`UIApplicationMain + 340
frame #51: 0x00000001abc90148 SwiftUI`___lldb_unnamed_symbol74307 + 168
frame #52: 0x00000001abc3c714 SwiftUI`___lldb_unnamed_symbol71237 + 152
frame #53: 0x00000001abc484d0 SwiftUI`___lldb_unnamed_symbol71675 + 132
frame #54: 0x0000000100ad44e4 JobShift`static JobShiftApp.$main(self=JobShift.JobShiftApp) at <compiler-generated>:0
frame #55: 0x0000000100ad45b8 JobShift`main at JobShiftApp.swift:6:8
frame #56: 0x00000001cb151e4c dyld`start + 2240
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack: (.....) libc++abi: terminating due to uncaught exception of type NSException
Upvotes: 2
Views: 89