简体   繁体   中英

UICollectionView crash on performBatchUpdates

I can't figure out what's the reason of this crash?

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)

Piece of code mentioned in crashlog is quite peaceful:

- (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];
    }];
}

To insert, delete, or move a single section or item, you must follow these steps:

Update the data in your data source object. Call the appropriate method of the collection view to insert or delete the section or item.

It is critical that you update your data source before notifying the collection view of any changes. The collection view methods assume that your data source contains the currently correct data. If it does not, the collection view might receive the wrong set of items from your data source or ask for items that are not there and crash your app.

This may be happening because you do need to also make sure with collectionViews to delete and insert sections. when you try to insert an item in a section that doesn't exist you will get this crash.

Preform Batch updates doesn't know that you meant to add section X+1 when you insert an item at X+1, X. without you already having added that section in.

make sure to use insert and delete section: https://developer.apple.com/documentation/uikit/uicollectionview/1618090-insertsections https://developer.apple.com/documentation/uikit/uicollectionview/1618102-deletesections

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM