0

我在UICollectionView使用UICollectionViewCompositionalLayout.list. 我的集合视图支持reorderingHandlers通过UICollectionViewDiffableDataSource. 此外,我正在混合使用NSDiffableDataSourceSnapshotand NSDiffableDataSourceSectionSnapshot,这是文档中允许的:

您可以将部分快照与 NSDiffableDataSourceSnapshot 一起使用或代替NSDiffableDataSourceSnapshot

NSDiffableDataSourceSnapshot在应用 a后跟 a后重新排序单元格时,我注意到一个奇怪的错误NSDiffableDataSourceSectionSnapshot

摘录UIViewController如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    // Omitted the code to build the UICollectionView, add to the view, and assign its dataSource.

    // Build and apply the initial snapshot, as a NSDiffableDataSourceSnapshot - NOT 
    // as a set of NSDiffableDataSourceSectionSnapshots.
    // For this example, we just use some arbitrary test data.
    var initialSnapshot = NSDiffableDataSourceSnapshot<Section, UUID>()
    for section in Section.allCases {
        initialSnapshot.appendSections([section])
        initialSnapshot.appendItems([UUID(), UUID(), UUID()], toSection: section)
    }
    dataSource.apply(initialSnapshot, animatingDifferences: false)

    // Then apply a section snapshot (same data as the initial snapshot).
    var sectionSnapshot = NSDiffableDataSourceSectionSnapshot<UUID>()
    sectionSnapshot.append(initialSnapshot.itemIdentifiers(inSection: .sectionTwo))
    self.dataSource.apply(sectionSnapshot, to: .sectionTwo)
}

我的数据源定义为:

private lazy var dataSource = makeDataSource()

func makeDataSource() -> UICollectionViewDiffableDataSource<Section, UUID> {
    let cellRegistration = makeCellRegistration()
    let dataSource = UICollectionViewDiffableDataSource<Section, UUID>(collectionView: collectionView) { view, indexPath, item in
        view.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
    }
    dataSource.reorderingHandlers.canReorderItem = { _ in true }
    return dataSource
}

我的最小可重现示例的全部内容包含在一个小型示例项目中。这包括单元格注册、构建UICollectionView、将其添加到视图等。

当我重新排序除应用了部分快照的部分之外的部分中的任何单元格时,我得到一个断言失败:

中的断言失败-[NSDiffableDataSourceSectionSnapshot snapshotOfParentItem:includingParentItem:],NSDiffableDataSourceSectionSnapshot.m:330

由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无效参数不满足:索引!= NSNotFound”

如下所示的调用堆栈表明这是在框架内发生的,以准备生成重新排序后的快照。

*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff203fbbb4 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff2019ebe7 objc_exception_throw + 48
    2   Foundation                          0x00007fff20750c12 _userInfoForFileAndLine + 0
    3   UIKitCore                           0x00007fff24f97699 -[NSDiffableDataSourceSectionSnapshot snapshotOfParentItem:includingParentItem:] + 755
    4   UIKitCore                           0x00007fff24340b8c -[_UIDiffableDataSourceSectionSnapshotRebaser _rebaseForInitialSnapshot:finalSnapshot:initialSectionSnapshots:dataSourceDiffer:shouldPerformChildSnapshotMoves:] + 2368
    5   UIKitCore                           0x00007fff24340218 -[_UIDiffableDataSourceSectionSnapshotRebaser initWithInitialSnapshot:finalSnapshot:initialSectionSnapshots:dataSourceDiffer:shouldPerformChildSnapshotMoves:] + 226
    6   UIKitCore                           0x00007fff24f23c67 +[NSDiffableDataSourceTransaction _computeReorderingTransactionWithInitialSnapshot:reorderingUpdate:sectionSnapshotProvider:] + 2051
    7   UIKitCore                           0x00007fff24607f54 -[__UIDiffableDataSource _reorderingTransactionForReorderingUpdate:] + 187
    8   UIKitCore                           0x00007fff24607c42 -[__UIDiffableDataSource _commitReorderingForItemAtIndexPath:toDestinationIndexPath:shouldPerformViewAnimations:] + 136
    9   libswiftUIKit.dylib                 0x00007fff59345148 $s5UIKit34UICollectionViewDiffableDataSourceC010collectionC0_10moveItemAt2toySo0bC0C_10Foundation9IndexPathVAKtFTf4dnnn_n + 72
    10  libswiftUIKit.dylib                 0x00007fff5933ef65 $s5UIKit34UICollectionViewDiffableDataSourceC010collectionC0_10moveItemAt2toySo0bC0C_10Foundation9IndexPathVAKtFTo + 165
    11  UIKitCore                           0x00007fff2468137c -[UICollectionView _notifyDataSourceForMoveOfItemFromIndexPath:toIndexPath:] + 273
    12  UIKitCore                           0x00007fff246800cd -[UICollectionView _completeInteractiveMovementWithDisposition:completion:] + 1686
    13  UIKitCore                           0x00007fff2461be1c -[UICollectionViewListCell _reorderControlDidEndReordering:cancelled:] + 85
    14  UIKitCore                           0x00007fff2461e92b -[_UICollectionViewListCellReorderControl endReordering:] + 64
    15  UIKitCore                           0x00007fff2461e5bc -[_UICollectionViewListCellReorderControl pan:] + 414
    16  UIKitCore                           0x00007fff24ad0f8f -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 49
    17  UIKitCore                           0x00007fff24adb0e9 _UIGestureRecognizerSendTargetActions + 100
    18  UIKitCore                           0x00007fff24ad7c55 _UIGestureRecognizerSendActions + 294
    19  UIKitCore                           0x00007fff24ad6f91 -[UIGestureRecognizer _updateGestureForActiveEvents] + 725
    20  UIKitCore                           0x00007fff24ac9213 _UIGestureEnvironmentUpdate + 2713
    21  UIKitCore                           0x00007fff24ac82f2 -[UIGestureEnvironment _updateForEvent:window:] + 902
    22  UIKitCore                           0x00007fff250449c9 -[UIWindow sendEvent:] + 5273
    23  UIKitCore                           0x00007fff2501b4e8 -[UIApplication sendEvent:] + 825
    24  UIKitCore                           0x00007fff250b128a __dispatchPreprocessedEventFromEventQueue + 8695
    25  UIKitCore                           0x00007fff250b3a10 __processEventQueue + 8579
    26  UIKitCore                           0x00007fff250aa1b6 __eventFetcherSourceCallback + 240
    27  CoreFoundation                      0x00007fff20369e25 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    28  CoreFoundation                      0x00007fff20369d1d __CFRunLoopDoSource0 + 180
    29  CoreFoundation                      0x00007fff203691f2 __CFRunLoopDoSources0 + 242
    30  CoreFoundation                      0x00007fff20363951 __CFRunLoopRun + 875
    31  CoreFoundation                      0x00007fff20363103 CFRunLoopRunSpecific + 567
    32  GraphicsServices                    0x00007fff2c851cd3 GSEventRunModal + 139
    33  UIKitCore                           0x00007fff24ffbe63 -[UIApplication _run] + 928
    34  UIKitCore                           0x00007fff25000a53 UIApplicationMain + 101
    35  libswiftUIKit.dylib                 0x00007fff5933d052 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
    36  diffable-experiments                0x0000000100759fe8 $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 104
    37  diffable-experiments                0x0000000100759f77 $s20diffable_experiments11AppDelegateC5$mainyyFZ + 39
    38  diffable-experiments                0x000000010075a068 main + 24
    39  dyld                                0x0000000100788e1e start_sim + 10
    40  ???                                 0x0000000000000001 0x0 + 1

我做错什么了吗?有解决方法吗?

4

0 回答 0