簡體   English   中英

在 performBatchUpdates 上 UICollectionView 崩潰

[英]UICollectionView crash on performBatchUpdates

我無法弄清楚這次崩潰的原因是什么?

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 處插入項目時,Preform Batch updates 不知道您打算添加部分 X+1,而您尚未添加該部分。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM