ios - UICollectionView 在 PerformBatchUpdates 上崩溃

标签 ios objective-c crash uicollectionview

我无法弄清楚这次崩溃的原因是什么?

Incident Identifier: BF4459C1-B97D-448E-AAE0-4DA6A1E4731C
CrashReporter Key:   E725A626-A25C-4923-8383-0BC7ECA7DADE
Hardware Model:      iPhone6,1
Process:         SampleProject [480]
Path:            /var/containers/Bundle/Application/458F9694-4BDA-4207-8C90-C2192520F8E2/SampleProject.app/SampleProject
Identifier:      com.mydomain.SampleProject
Version:         1.3.0 (1.3.0.36)
Code Type:       ARM-64
Parent Process:  ??? [1]

Date/Time:       2017-05-17T04:11:03Z
Launch Time:     2017-05-17T04:06:51Z
OS Version:      iPhone OS 10.3.1 (14E304)
Report Version:  104

Exception Type:  SIGSEGV
Exception Codes: SEGV_ACCERR at 0xfffffffffffffff8
Crashed Thread:  0

Application Specific Information:
Selector name found in current argument registers: _setLayoutAttributes:atGlobalItemIndex:

Thread 0 Crashed:
0   UIKit                                0x000000018de7d38c -[UICollectionViewData _setLayoutAttributes:atGlobalItemIndex:] + 80
1   UIKit                                0x000000018de7ac94 __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 1892
2   UIKit                                0x000000018de79f68 -[UICollectionViewData validateLayoutInRect:] + 1492
3   UIKit                                0x000000018de8023c -[UICollectionViewData layoutAttributesForElementsInRect:] + 264
4   UIKit                                0x000000018de7dbc0 -[UICollectionView _updateVisibleCellsNow:] + 548
5   UIKit                                0x000000018dfea058 -[UICollectionView _setupCellAnimations] + 28
6   UIKit                                0x000000018dff459c -[UICollectionView _beginUpdates] + 44
7   UIKit                                0x000000018e72c8c0 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:] + 256
8   UIKit                                0x000000018e72c79c -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 92
9   UIKit                                0x000000018e72c720 -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 80
10  UIKit                                0x000000018dff4558 -[UICollectionView performBatchUpdates:completion:] + 60
11  SampleProject                               0x000000010002a4b8 -[InvitesDataSource didDeleteDataAtIndexes:loadDataAtIndexes:] (InvitesDataSource.m:50)
12  SampleProject                               0x0000000100092f58 -[DialogsController _didReceiveOldestInvites:] (DialogsController.m:311)
13  SampleProject                               0x0000000100092500 -[DialogsController _didReceiveOldestDialogs:ofType:] (DialogsController.m:248)
14  SampleProject                               0x000000010004aad8 -[DialogsController(Notifications) dialogListDidReceived:] (DialogsController+Notifications.m:94)

崩溃日志中提到的一段代码非常平静:

- (void)didDeleteDataAtIndexes:(NSIndexSet *)deleted loadDataAtIndexes:(NSIndexSet *)loaded {    
    [self.collectionView performBatchUpdates:^{
        [deleted enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
            [self.collectionView deleteItemsAtIndexPaths:[self _indexPathsForIndex:idx]];
        }];

        [loaded enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
            [self.collectionView insertItemsAtIndexPaths:[self _indexPathsForIndex:idx]];
        }];

    } completion:^(BOOL finished) {
        [self.collectionView reloadData];
    }];
}

最佳答案

这可能会发生,因为您确实还需要确保使用 collectionViews 来删除和插入部分。当您尝试在不存在的部分中插入项目时,您将遇到此崩溃。

当您在 X+1、X 处插入项目时,预制批量更新不知道您打算添加 X+1 部分,而您尚未添加该部分。

确保使用插入和删除部分: https://developer.apple.com/documentation/uikit/uicollectionview/1618090-insertsections https://developer.apple.com/documentation/uikit/uicollectionview/1618102-deletesections

关于ios - UICollectionView 在 PerformBatchUpdates 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44311117/

相关文章:

ios - NSKeyedArchiver archiveRootObject 一直返回 NO

iOS 约束方程值

ios - Objective-C:应用内消息按钮的 Action

c# - 防止 C# 由于无效用户输入而崩溃

ios - XMPPFramework - 如何创建 MUC 房间并邀请用户?

ios - iOS从Crashlytics迁移到FirebaseCrashlytics

ios - 如何取消选择单选按钮(UIButton)的先前状态

ios - 如何在 XIB 中正确加载和显示自定义 UIView?

指向 Crashed : com. twitter.crashlytics.ios.exception 的 iOS App Crashes

ios - iOS意外退出的方式有哪些