How to arrange movements, inserts, deletions and updates in the UICollectionView executeBatchUpdates execution block?

In my UICollectionView I use a simple array of custom objects to create and display cells. Sometimes this data changes, and I would like to revive the changes immediately. I decided to do this by tracking all the changes in the second array, distinguishing them and creating a set of move, insert, delete and update operations inside the performBatchUpdates block. Now I understand that it is quite difficult to do all this in one block, because you need to worry about the order of operations with indexes. In fact, the accepted answer to this problem is incorrect (but corrected in the comments).

The documentation seems pretty missing, but it covers one case:

Deletions are processed before being inserted into batch operations. This means indexes for deletions are processed relative to the collection view indices displayed before the batch operation, and indexes for insertions are processed relative to the state indexes after all deletions in the batch operation.

However, the document does not say when transfers are processed. If I call moveItemAtIndexPath and deleteItemsAtIndexPaths in the same performBatchUpdates , should the move indices be in order before or after deletion? What about insertItemsAtIndexPaths ?

Finally, I ran into problems causing reloadItemsAtIndexPaths and moveItemAtIndexPath in the same operation:

Fatal Exception: NSInternalInconsistencyException is trying to delete and reload the same pointer path

Is there a way to do all the operations I want in the same performBatchUpdates ? If so, what update order is processed relative to others? If not, what do people usually do? Reload data after all other operations? Before? I would prefer that all animations happen in one step.

+7
ios objective-c uikit swift uicollectionview
source share
1 answer

For relocation operations, indexPath is the pre-delete index, and indexPath is the post-delete index. Reloads should only be specified for indexed indexes that have not been inserted, deleted, or moved. This is probably why you are seeing an NSInternalInconsistencyException .

A convenient way to check operations is configured correctly: the set of reloads, insertion and movement in indexPaths should not have duplicates, and the set of reloads, deletion and movement from indexPaths should not have duplicates.

UPDATE:

It seems that the items you are moving are also not being updated, but only being moved. So, if you need to update and move an item, you can reboot before or after the batch update (depending on the state of the data source).

+5
source share

All Articles