Skip to content
This repository was archived by the owner on Apr 15, 2020. It is now read-only.
This repository was archived by the owner on Apr 15, 2020. It is now read-only.

Allow supplementary views for empty sections in collection view #38

@f1nality

Description

@f1nality

Hello,

My app crashes when fetch request is empty and I have supplementary views (without supplementary views everything ok). I get:
index 0 beyond bounds for empty NSArray

Seems like the problem is in CollectionViewDataSourceProvider:

        supplementaryViewAtIndexPath: { [unowned self] (collectionView, kind, indexPath) -> UICollectionReusableView in
            if let factory = self.supplementaryViewFactory {
                let item = self.sections[indexPath.section].items[indexPath.row]
                let view = factory.supplementaryViewForItem(item, kind: kind, inCollectionView: collectionView, atIndexPath: indexPath)
                return factory.configureSupplementaryView(view, forItem: nil, kind: kind, inCollectionView: collectionView, atIndexPath: indexPath)
            }

            // we must not return nil here, per the `UICollectionViewDataSource` docs
            // however, this will never get called as it is the client's responsibilty
            // supplementary views are hidden by returning `CGSizeZero`
            // from `collectionView(_:layout:referenceSizeForHeaderInSection:)`
            fatalError("Attempt to dequeue unknown or invalid supplementary view <\(kind)> "
                + "for collection view <\(collectionView)> at indexPath <\(indexPath)>")
        })

Stacktrace:

*** First throw call stack:
(
    0   CoreFoundation                      0x000000010bf640b5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010b66bdeb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010bf12ae5 -[__NSArray0 objectAtIndex:] + 101
    3   my_app                              0x000000010a79dda3 _TFFC6my_app46CollectionViewFetchedResultsDataSourceProvidergP33_6A7F8A132E7132932DD193987465D6ED17bridgedDataSourceCS_P33_6A7F8A132E7132932DD193987465D6ED31BridgedCollectionViewDataSourceU2_FTCSo16UICollectionViewSSCSo11NSIndexPath_CSo24UICollectionReusableView + 835
    4   my_app                              0x000000010a79b9e8 _TPA__TFFC6my_app46CollectionViewFetchedResultsDataSourceProvidergP33_6A7F8A132E7132932DD193987465D6ED17bridgedDataSourceCS_P33_6A7F8A132E7132932DD193987465D6ED31BridgedCollectionViewDataSourceU2_FTCSo16UICollectionViewSSCSo11NSIndexPath_CSo24UICollectionReusableView + 280
    5   my_app                              0x000000010a79aa49 _TFC6my_appP33_6A7F8A132E7132932DD193987465D6ED31BridgedCollectionViewDataSource14collectionViewfS0_FTCSo16UICollectionView33viewForSupplementaryElementOfKindSS11atIndexPathCSo11NSIndexPath_CSo24UICollectionReusableView + 153
    6   my_app                              0x000000010a79aaec _TToFC6my_appP33_6A7F8A132E7132932DD193987465D6ED31BridgedCollectionViewDataSource14collectionViewfS0_FTCSo16UICollectionView33viewForSupplementaryElementOfKindSS11atIndexPathCSo11NSIndexPath_CSo24UICollectionReusableView + 108
    7   UIKit                               0x000000010cd81d51 -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:] + 679
    8   UIKit                               0x000000010cd838b8 -[UICollectionView _updateVisibleCellsNow:] + 4487
    9   UIKit                               0x000000010cd87fdb -[UICollectionView layoutSubviews] + 247
    10  UIKit                               0x000000010c5ece6c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
    11  QuartzCore                          0x000000011264b56a -[CALayer layoutSublayers] + 146
    12  QuartzCore                          0x000000011263fe40 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    13  UIKit                               0x000000010c5dcacd -[UIView(Hierarchy) layoutBelowIfNeeded] + 1083
    14  UIKit                               0x000000010c70fad6 -[UINavigationController _layoutViewController:] + 1613
    15  UIKit                               0x000000010c70e916 -[UINavigationController _layoutTopViewController] + 268
    16  UIKit                               0x000000010c70bc1f -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 856
    17  UIKit                               0x000000010c9aadfc -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 422
    18  UIKit                               0x000000010c9ab172 -[UINavigationTransitionView _cleanupTransition] + 788
    19  UIKit                               0x000000010c5c0b17 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 241
    20  UIKit                               0x000000010c5bdb62 +[UIViewAnimationState popAnimationState] + 314
    21  UIKit                               0x000000010c9aaaad -[UINavigationTransitionView transition:fromView:toView:] + 2640
    22  UIKit                               0x000000010c710b65 -[UINavigationController _startTransition:fromViewController:toViewController:] + 3291
    23  UIKit                               0x000000010c711161 -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
    24  UIKit                               0x000000010c712177 -[UINavigationController __viewWillLayoutSubviews] + 57
    25  UIKit                               0x000000010c8b8209 -[UILayoutContainerView layoutSubviews] + 248
    26  UIKit                               0x000000010c5ece6c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
    27  QuartzCore                          0x000000011264b56a -[CALayer layoutSublayers] + 146
    28  QuartzCore                          0x000000011263fe40 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    29  QuartzCore                          0x000000011263fcbe _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    30  QuartzCore                          0x0000000112634445 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
    31  QuartzCore                          0x0000000112661bda _ZN2CA11Transaction6commitEv + 486
    32  QuartzCore                          0x000000011266234c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    33  CoreFoundation                      0x000000010be8fab7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    34  CoreFoundation                      0x000000010be8fa27 __CFRunLoopDoObservers + 391
    35  CoreFoundation                      0x000000010be84f9c CFRunLoopRunSpecific + 524
    36  UIKit                               0x000000010c533275 -[UIApplication _run] + 402
    37  UIKit                               0x000000010c53808d UIApplicationMain + 171
    38  my_app                              0x000000010a7854fd main + 109
    39  libdyld.dylib                       0x000000010ee6f9e9 start + 1
    40  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions